{{announcement.body}}
{{announcement.title}}

Java Enterprise Annotations Part 1

DZone 's Guide to

Java Enterprise Annotations Part 1

In this article, we discuss popular Java annotations to sure up your understanding of this common pattern within enterprise applications.

· Java Zone ·
Free Resource

Most enterprise Java applications are driven by annotations. Java annotations encapsulate many different functionalities. Here, I'll introduce some of the most popular annotations and explain what they are responsible for to sure up your understanding of annotations you're already familiar with and introduce you to ones you may not know. 

Inversion of Control and Dependency Injection

These two patterns are responsable for bean initialization. IOC initializes beans and defines dependencies between them, while DI allows you to use them in your class without calling a constructor. There's much more you can do with these, but for the sake of brevity, we'll stop here. 

IOC and DI in Spring Boot

Java
 




x


 
1
@Configuration
2
public class Config {
3
    @Bean
4
    public Service service()
5
    {
6
        return new ServiceImplementation();
7
    }
8
}


Java
 




xxxxxxxxxx
1


 
1
@Component
2
public class YourClass {
3
    @Autowired
4
    private Service service;
5
 
          
6
    public void printData(){
7
        System.out.println(service.getData());
8
    }
9
}



In the first code block, we define the implementation we use for the Service interface. At the bottom, we inject the Service interface implementation using field dependency injection. So, annotations @Configuration, @Bean, @Component, are responsible for IOC. @Autowired is responsible for dependency injection. Spring is not the only IOC and DI framework. 

The most popular IOC and DI annotations:

  • Java EE: @Alternative, @Qualifier, @Inject,  @Named.
  • Google Guice: @Inject, @Named  (not too much because it uses bind function instead).
  • Dagger: @Provides, @Module, @Component, @Inject, @Named.
  • Spring:  @Autowired , @Qualifier, @Component, @Bean.
You may also like: How Do Annotations Work in Java?

JPA (Java Persistent Api)

JPA annotations declare communication between an application and a database. Instead of JDBC queries, JPA provides access using entity classes. For example:

Java
 




x
22


 
1
@Entity
2
@Table(name = "employees", schema = "testdb")
3
public class EmployeesEntity {
4
    private int empNo;
5
    private Date birthDate;
6
    private String firstName;
7
    @Id
8
    @Column(name = "emp_no", nullable = false)
9
    public int getEmpNo() {
10
        return empNo;
11
    }
12
    @Basic
13
    @Column(name = "first_name", nullable = false, length = 14)
14
    public String getFirstName() {
15
        return firstName;
16
    }
17
    @Basic
18
    @Column(name = "birth_date", nullable = false)
19
    public Date getBirthDate() {
20
        return birthDate;
21
    }
22
}



Now, take a look into the corresponding SQL table schema:

MySQL
 




xxxxxxxxxx
1


 
1
CREATE TABLE `employees` (
2
  `emp_no` int NOT NULL,
3
  `birth_date` date NOT NULL,
4
  `first_name` varchar(14) NOT NULL,
5
  PRIMARY KEY (`emp_no`)
6
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4



As you can see, the class uses the @Table annotation to map to the table name and the @Column annotation to map to columns.

Most popular jpa annotations: 

  • @Entity 
  • @Table 
  • @Column 
  • @Id 
  • @GeneratedValue 
  • @OneToOne 
  • @OneToMany 
  • @ManyToMany 
  • @Basic 
  • @Transient

REST 

REST is the most popular word to describe jax-rs and Java EE specifications (a.k.a. web services specifications). The jax-rs standard became a significant improvement over servlets. Let's take a look at an example with Spring.

Java
 




xxxxxxxxxx
1


1
@GetMapping("/resource")
2
public ResponseEntity<String> simpleRestGetMethod(@RequestParam String someParam) {
3
  return ResponseEntity.ok().build();
4
}



In this example, @GetMapping maps HTTP requests with the URL "/resource" with the simpleRestGetMethod. @RequestParam is also passed in to convert the attribute from a request body to string someParam variable. 

Most popular rest annotations: 

  • Java EE:  @Path, @POST,@PUT,@GET, @Produces, @Consumes, @PathParam, @QueryParam.
  • Spring: @RequestMapping, @PostMapping, @PutMapping, @GetMapping, @Produces, @Consumes, @PathParam, @QueryParam.


Further Reading

Topics:
annotations ,dependecy injection ,java ,javaee ,jpa ,rest

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}