Spring Boot Annotations Everyone Must Know in 2023

One of Spring Boot's great features is the extensive use of annotations which streamline development, improving readability and minimizing boilerplate code. Here are the Spring Boot annotations every developer should be familiar with:

Core Annotations:

  1. @SpringBootApplication: A combination of three annotations – @Configuration, @ComponentScan, and @EnableAutoConfiguration. This annotation is typically used in the main class and triggers auto-configuration, component scanning, and configuration properties.

  2. @Component: The most generic annotation indicating that a class is a Spring component. Its subclasses (@Service, @Repository, @Controller, and @RestController) provide more specificity about the component's role.

  3. @Autowired: Enables automatic dependency injection. Spring will automatically inject the declared beans by matching data type, property name, or qualifier.

  4. @Bean: Denotes a method that produces a bean to be managed by the Spring container.

Web Layer Annotations:

  1. @RestController: Combines @Controller and @ResponseBody. Indicates the class is a controller where every method returns a domain object instead of a view.

  2. @RequestMapping: Maps a URL pattern to a method or class. Can also specify the HTTP method (GET, POST, etc.).

  3. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping: Specific shortcuts for @RequestMapping to handle the respective HTTP methods.

  4. @PathVariable: Indicates that a method parameter is bound to a URI template variable.

  5. @RequestBody: Indicates that a method parameter should be bound to the body of an HTTP request.

Data Access Annotations:

  1. @Entity: Marks a class as a JPA entity. Used for creating database tables.

  2. @Repository: Indicates that the class is a Data Access Object (DAO) component.

  3. @Transactional: Indicates that a method or class should have transactional boundaries.

  4. @JpaRepository: An interface that provides generic CRUD operations on an entity. Often used in combination with Spring Data JPA.

Configuration & Properties:

  1. @Configuration: Marks a class as a source of bean definitions.

  2. @ConfigurationProperties: Binds and validates external configurations (like application.properties or application.yml) to a Java object.

  3. @Value: Injects values from properties files.

Aspect-Oriented Programming (AOP):

  1. @Aspect: Defines an aspect, which can include pointcuts and advices to modularize cross-cutting concerns in applications.

  2. @Before, @After, @Around: Specifies when the advices should run with respect to the method they are applied to.

Miscellaneous:

  1. @Profile: Indicates that a component or configuration is only active for a specific profile. Useful for segregating parts of logic for different environments like dev, test, prod, etc.

  2. @Scheduled: Indicates that a method should be executed at fixed intervals or at a specific time of day.

Conclusion

These annotations are just the tip of the iceberg. Spring Boot and the broader Spring ecosystem have a plethora of annotations tailored for specific needs. However, having a strong grasp of the annotations listed above will give you a solid foundation to build effective and efficient Spring Boot applications in 2023.

Did you find this article valuable?

Support Adrian Kodja by becoming a sponsor. Any amount is appreciated!