In the previous article, we discussed different spring core annotations. Here we are discussing the main stereotype annotations in Spring.
The Spring Framework provides you with some special annotations. These annotations are used to create Spring beans automatically in the application context. The main stereotype annotation is
There are some Stereotype meta-annotations which is derived from
@Component those are
@Service: Used to create Spring beans at the Service layer,
@Repository: Which is used to create Spring beans for the repositories at the DAO layer, and
@Controller: Which is used to create Spring beans at the controller layer.
The stereotype annotations in spring are @Component, @Service, @Repository and @Controller.
This annotation is used in classes to indicate a Spring component. The
@Component annotation marks the Java class as a bean or says component so that the component-scanning mechanism of Spring can add to the application context.
What is the difference between @Component and @Bean annotation?
Both of these annotations are used for creating spring beans. Here follows the major comparison points between @Component and @Bean annotations
- @Component auto-detects and configures the beans using classpath scanning whereas @Bean explicitly declares a single bean, So if you want to create a bean for the class then you need to create a method that returns the class instance and needs to declare that method with @Bean annotation.
- @Component does not decouple the declaration of the bean from the class definition whereas @Bean decouples the declaration of the bean from the class definition. So we can keep the class that is completely independent of spring dependency. Mainly the @Bean annotation is used for added spring beans for the class which is defined on external jars.
- @Component is a class-level annotation whereas @Bean is a method level annotation and the name of the method serves as the bean name.
- @Component need not be used with the @Configuration annotation whereas @Bean annotation has to be used within the class which is annotated with @Configuration.
- We cannot create a bean of a class using @Component if the class is outside the spring container whereas we can create a bean of a class using @Bean even if the class is present outside the spring container.
What is Special about the @Component annotation as compared with other stereotype annotations.
<context:component-scan> only scans
@Component and does not look for
@Repository in general. They are scanned because they themselves are annotated with
Also if you look at the source of other stereotypes then you can see all other stereotypes are components. Which you can see on going forward.
@Service is a stereotype for the service layer. The @Service marks a Java class that performs some service, such as execute business logic, perform calculations and call external APIs. This annotation is a specialized form of the @Component annotation intended to be used in the service layer.
Here below explains the code for the
@Service annotation. Here you can see it is added
@Component annotation in the code. which means
@Service annotation is a kind of
We mark beans with
@Service to indicate that it’s holding the business logic. As compared with
@Component annotation, there’s not any other specialty except using it in the service layer. But we can expect Spring may add some additional exceptional in future.
This annotation is used on Java classes that directly access the database. The @Repository annotation works as a marker for any class that fulfills the role of repository or Data Access Object.
@Repository is a kind of
@Component annotation. Here below explains the code for
@Repository’s job is to catch persistence specific exceptions and rethrow them as one of Spring’s unified unchecked exception.
@Repository’s job is to catch platform-specific exceptions and re-throw them as one of Spring’s unified unchecked exceptions. For this, Spring provided with
PersistenceExceptionTranslationPostProcessor, that required to add in our Spring’s application context like this:
This bean post-processor adds an advisor to any bean that’s annotated
@Repository so that any platform-specific exceptions are caught and then re-thrown as one of Spring’s unchecked data access exceptions.
@Controller spring bean is also a kind of spring component. Here below explains the code for
We cannot switch this annotation with any other like
@Repository, even they look the same. The dispatcher scans the classes annotated with
@Controller and detects methods annotated with
@RequestMapping annotations within them. We can use
@RequestMapping on/in only those methods whose classes are annotated with
@Controller and it will NOT work with