Spring Boot provides powerful annotations to simplify development. Here’s a categorized list with explanations and examples.
Marks the main class of a Spring Boot application.
Internally includes:
@Configuration
– Marks a configuration class@EnableAutoConfiguration
– Enables auto-configuration@ComponentScan
– Scans for Spring components@Controller
is a Spring MVC annotation that marks a class as a web controller handling HTTP requests.
✅ Best Use Case: Use @Controller
for handling MVC views (Thymeleaf, JSP, etc.).
A combination of @Controller
+ @ResponseBody
, used for REST APIs.
Difference between @Controller
and @RestController
:
@Controller
is used for MVC-based web applications (returns views).@RestController
is used for RESTful APIs (returns JSON/XML responses).@Service
is a specialized version of @Component
, specifically for service-layer logic. It indicates that a class contains business logic.
✅ Best Use Case: Use @Service
for business logic or service classes that interact with repositories.
Used for Hibernate/JPA entity mapping.
@Repository
is a specialized @Component
used for database access logic.
It is mainly used with Spring Data JPA and Hibernate.
✅ Best Use Case: Use @Repository
for DAO (Data Access Object) classes that interact with the database.
path
, method
, produces
, and consumes
.🔹 Alternative: Instead of @RequestMapping(method = RequestMethod.GET)
, you can use @GetMapping
, which is more specific.
✔ Can also accept request body using @RequestBody
Extracts values from the URL path or query parameters.
Used for validating request body data.
Injects a dependency automatically.
Used when multiple beans of the same type exist.
Marks a bean as the default when multiple beans of the same type exist.