Stereotype Annotation
About
Stereotype annotations in Spring are meta-annotations used to define Spring-managed components. These annotations serve as indicators for Spring to automatically detect, register, and manage beans in the application context.
The term "stereotype" means a predefined role or category. In Spring, these annotations define the role of a class in the application, such as a service, controller, or repository.
List of Stereotype Annotations in Spring
@Component
Generic annotation to mark any class as a Spring-managed bean.
@Service
Used for service-layer components, indicating business logic processing.
@Repository
Used for data access components, integrates with JPA, Hibernate, and exception translation.
@Controller
Used in Spring MVC to handle web requests.
@RestController
A specialization of @Controller
that returns JSON/XML responses.
@Component
– The Base Stereotype Annotation
@Component
– The Base Stereotype Annotation@Component
is the most generic stereotype annotation. It marks a class as a Spring-managed bean, making it eligible for dependency injection.
Example
Spring automatically registers
EmailService
as a bean, without requiring@Bean
.Injecting
EmailService
in another class:
@Service
– Business Logic Layer
@Service
– Business Logic Layer@Service
is a specialization of @Component
used for business logic services. It provides semantic clarity and can be extended in the future for AOP (Aspect-Oriented Programming) or transaction management.
Example
Functionally, it behaves like
@Component
, but it clearly represents business logic.
@Repository
– Data Access Layer
@Repository
– Data Access Layer@Repository
is used for DAO (Data Access Object) components. It provides exception translation, converting database exceptions into Spring’s DataAccessException
hierarchy.
Example
Spring automatically translates database-related exceptions when using
@Repository
.Works seamlessly with JPA, Hibernate, and JDBC.
@Controller
– MVC Controller Layer
@Controller
– MVC Controller Layer@Controller
is used in Spring MVC to handle HTTP requests and return views (JSP, Thymeleaf, etc.).
Example
Returns a view name instead of a response body.
Should be used in Spring MVC applications.
@RestController
– REST API Layer
@RestController
– REST API Layer@RestController
is a combination of @Controller
+ @ResponseBody
. It is used to build RESTful APIs.
Example
Eliminates the need for
@ResponseBody
.Returns JSON/XML directly instead of a view.
Why Use Stereotype Annotations?
Eliminates the need for manual bean registration – Spring auto-detects and registers beans.
Promotes clear separation of concerns – Different annotations represent different layers (e.g.,
@Service
for business logic).Enhances maintainability – Easy to locate and manage different components.
Works seamlessly with Spring Boot – Enables component scanning, reducing XML configuration.
How Stereotype Annotations Enable Component Scanning?
Spring automatically scans for these annotations using component scanning.
Enable Component Scanning (If not using Spring Boot):
Spring Boot automatically enables scanning for the main package and sub-packages.
Custom Stereotype Annotations
We can create custom stereotype annotations by combining existing ones.
Example: Custom
@UseCase
Annotation
Usage
Now, PaymentProcessing
is a Spring bean, but with better semantics.
Last updated
Was this helpful?