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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones Build AI Agents That Are Ready for Production
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
Build AI Agents That Are Ready for Production

LIVE: “Cognitive Databases, Intelligent Data: Unified Infrastructure for Vector Search, AI-Optimized Queries, & Hybrid Workloads" Report

Join this live webinar to learn safer rollout techniques for schema changes, index testing, and database migrations.

Live Webinar: Exclusive practitioner summit on AI-powered CDN operations and real-world automation strategies

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

  • DZone's Article Submission Guidelines
  • How to Submit a Post to DZone
  • Workflows vs AI Agents vs Multi-Agent Systems: A Practical Guide for Developers
  • A Spring Boot App With Half the Startup Time
  1. DZone
  2. Coding
  3. Languages
  4. Java Enterprise Annotations Part 1

Java Enterprise Annotations Part 1

By 
Dmitry Egorov user avatar
Dmitry Egorov
DZone Core CORE ·
Mar. 12, 20 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
13.6K Views

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

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

The likes didn't load as expected. Please refresh the page and try again.

  • 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