DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring Microservice Tip: Abstracting the Database Hostname With Environment Variable
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Composing Custom Annotations in Spring
  • Testcontainers With Kotlin and Spring Data R2DBC

Trending

  • Why Your AI Agent Works in the Demo and Fails in Production
  • The Twelve-Factor Agents: Building Production-Ready LLM Applications
  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Java Enterprise Annotations Part 1

Java Enterprise Annotations Part 1

By 
Dmitry Egorov
DZone Core CORE ·
Mar. 12, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Creating Annotations in Java.
  • A Guide to Spring Framework Annotations.
  • A First Look at Records in Java 14.
Annotation Java (programming language) Spring Framework Database

Opinions expressed by DZone contributors are their own.

Related

  • Spring Microservice Tip: Abstracting the Database Hostname With Environment Variable
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Composing Custom Annotations in Spring
  • Testcontainers With Kotlin and Spring Data R2DBC

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook