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
public class Config {
public Service service()
{
return new ServiceImplementation();
}
}
xxxxxxxxxx
public class YourClass {
private Service service;
public void printData(){
System.out.println(service.getData());
}
}
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:
name = "employees", schema = "testdb") (
public class EmployeesEntity {
private int empNo;
private Date birthDate;
private String firstName;
name = "emp_no", nullable = false) (
public int getEmpNo() {
return empNo;
}
name = "first_name", nullable = false, length = 14) (
public String getFirstName() {
return firstName;
}
name = "birth_date", nullable = false) (
public Date getBirthDate() {
return birthDate;
}
}
Now, take a look into the corresponding SQL table schema:
xxxxxxxxxx
CREATE TABLE `employees` (
`emp_no` int NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
PRIMARY KEY (`emp_no`)
) 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.
xxxxxxxxxx
"/resource") (
public ResponseEntity<String> simpleRestGetMethod( String someParam) {
return ResponseEntity.ok().build();
}
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
- Creating Annotations in Java.
- A Guide to Spring Framework Annotations.
- A First Look at Records in Java 14.