When learning Spring Boot, you will notice that many classes, methods, and fields contain annotations such as @RestController, @Service, @Autowired, and @Entity.
At first, these annotations may look confusing. But once you understand what annotations are and why we use them, Spring Boot becomes much easier to understand.
In this article, we will explore what annotations are, their advantages, and the most commonly used Spring Boot annotations in simple language.
1. What is an annotation?
An annotation is a way of providing additional information (metadata) about our Java code.
Annotations begin with the @ symbol. They can be applied to classes, methods, fields, constructors, and other elements.
For example, @Service tells Spring that a particular class is a service component. @GetMapping tells Spring which HTTP GET requests a method should handle.
Annotations don't usually perform the task themselves. Instead, frameworks like Spring read these annotations and perform the required actions.
2. Why do we use annotations?
Annotations provide several advantages:
- Less configuration: We don't need to write large configuration files for many common tasks.
- Less boilerplate code: Spring handles many tasks automatically.
- Better readability: We can easily understand the purpose of a class or method by looking at its annotations.
- Easy development: They simplify configuring application components, handling requests, database mapping, and dependency injection.
- Maintainability: They help organize application behavior and configuration.
3. Common Spring Boot annotations
Application startup
| Annotation | What it does |
|---|---|
@SpringBootApplication | Marks the main application class and enables Spring Boot configuration, auto-configuration, and component scanning. |
Component annotations
These annotations tell Spring to manage the objects of the annotated classes.
| Annotation | What it does |
|---|---|
@Component | Marks a general-purpose class as a Spring-managed component. |
@Service | Identifies a class that contains business logic. |
@Repository | Identifies a class responsible for database operations. |
@Controller | Handles web requests and usually returns webpage views. |
@RestController | Handles REST API requests and returns response data, commonly JSON. |
Dependency injection
These annotations help Spring connect objects and provide required dependencies.
| Annotation | What it does |
|---|---|
@Autowired | Tells Spring to inject a required dependency. |
@Qualifier | Specifies which bean to inject when multiple beans of the same type exist. |
@Primary | Marks a bean as the preferred choice when multiple beans of the same type exist. |
HTTP request handling
These annotations are commonly used when building REST APIs.
| Annotation | What it does |
|---|---|
@RequestMapping | Maps HTTP requests to a controller or method. |
@GetMapping | Handles HTTP GET requests, usually to retrieve data. |
@PostMapping | Handles HTTP POST requests, usually to create data. |
@PutMapping | Handles HTTP PUT requests, usually to update data. |
@DeleteMapping | Handles HTTP DELETE requests, usually to delete data. |
@PathVariable | Extracts a value from the URL path. |
@RequestParam | Extracts a query parameter from the URL. |
@RequestBody | Converts request body data, such as JSON, into a Java object. |
@CrossOrigin | Allows cross-origin requests from permitted origins. |
Database and JPA annotations
These annotations help map Java classes and fields to database tables and columns.
| Annotation | What it does |
|---|---|
@Entity | Marks a Java class as a database entity. |
@Table | Specifies the database table name for an entity. |
@Id | Identifies the primary key field. |
@GeneratedValue | Configures how primary key values are generated. |
@Column | Configures a database column, such as its name or constraints. |
@OneToMany | Defines a one-to-many relationship between entities. |
@ManyToOne | Defines a many-to-one relationship between entities. |
@OneToOne | Defines a one-to-one relationship between entities. |
@ManyToMany | Defines a many-to-many relationship between entities. |
@JoinColumn | Specifies a database column used to join or reference related entities. |
@Enumerated | Specifies how enum values are stored in the database. |
Configuration annotations
These annotations help configure the application and its objects.
| Annotation | What it does |
|---|---|
@Configuration | Marks a class as a configuration class. |
@Bean | Registers an object returned by a method as a Spring-managed bean. |
@Value | Injects values from configuration files or supported property sources. |
Other useful annotations
| Annotation | What it does |
|---|---|
@Transactional | Manages database transactions, allowing related operations to succeed or roll back together. |
4. How Spring uses annotations
When a Spring Boot application starts, Spring scans the configured packages and reads relevant annotations.
Based on those annotations, Spring can:
- Create and manage objects (beans).
- Connect dependencies between objects.
- Configure HTTP endpoints.
- Map Java entities to database tables.
- Apply configurations and other framework behaviors.
For example, when Spring finds @Service on a class, it can register that class as a bean. When it finds @GetMapping, it registers the method as a handler for a particular GET request.
This is how annotations help Spring Boot handle many tasks automatically.
Conclusion
Annotations are an important part of Spring Boot because they simplify configuration and reduce the amount of code developers need to write.
You don't need to memorize all annotations at once. Start by understanding the annotations you encounter in your projects, their purpose, and how Spring uses them.
Once you become familiar with the common annotations, reading and writing Spring Boot applications becomes much easier.
Happy coding! 🚀
