Java Enterprise Annotations Part 1

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:

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: 

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: 


Further Reading

 

 

 

 

Top