Java Enterprise Annotations Part 1
Join the DZone community and get the full member experience.Join For Free
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
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
@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:
- Google Guice:
@Named(not too much because it uses bind function instead).
@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:
Now, take a look into the corresponding SQL table schema:
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 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.
In this example,
@GetMapping maps HTTP requests with the URL "/resource" with the
@RequestParam is also passed in to convert the attribute from a request body to string someParam variable.
Most popular rest annotations:
- Java EE:
Opinions expressed by DZone contributors are their own.