What’s the difference between @Component, @Repository and @Service annotations in Spring? Can @Component, @Repository, and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device? In other words, if I ...
Home/repository
We'll here focus on some minor differences among them. First the Similarity First point worth highlighting again is that with respect to scan-auto-detection and dependency injection for BeanDefinition all these annotations (viz., @Component, @Service, @Repository, @Controller) are the same. We can uRead more
We’ll here focus on some minor differences among them.
Differences between @Component, @Repository, @Controller and @Service
This is a general-purpose stereotype annotation indicating that the class is a spring component.
What’s special about @Component
<context:component-scan>only scans@Componentand does not look for@Controller,@Serviceand@Repositoryin general. They are scanned because they themselves are annotated with@Component.Just take a look at
@Controller,@Serviceand@Repositoryannotation definitions:Thus, it’s not wrong to say that
@Controller,@Serviceand@Repositoryare special types of@Componentannotation.<context:component-scan>picks them up and registers their following classes as beans, just as if they were annotated with@Component.Special type annotations are also scanned, because they themselves are annotated with
@Componentannotation, which means they are also@Components. If we define our own custom annotation and annotate it with@Component, it will also get scanned with<context:component-scan>This is to indicate that the class defines a data repository.
What’s special about @Repository?
In addition to pointing out, that this is an Annotation based Configuration,
@Repository’s job is to catch platform specific exceptions and re-throw them as one of Spring’s unified unchecked exception. For this, we’re provided withPersistenceExceptionTranslationPostProcessor, that we are required to add in our Spring’s application context like this:This bean post processor adds an advisor to any bean that’s annotated with
@Repositoryso that any platform-specific exceptions are caught and then re-thrown as one of Spring’s unchecked data access exceptions.The
@Controllerannotation indicates that a particular class serves the role of a controller. The@Controllerannotation acts as a stereotype for the annotated class, indicating its role.What’s special about @Controller?
We cannot switch this annotation with any other like
@Serviceor@Repository, even though they look same. The dispatcher scans the classes annotated with@Controllerand detects methods annotated with@RequestMappingannotations within them. We can use@RequestMappingon/in only those methods whose classes are annotated with@Controllerand it will NOT work with@Component,@Service,@Repositoryetc…Note: If a class is already registered as a bean through any alternate method, like through
@Beanor through@Component,@Serviceetc… annotations, then@RequestMappingcan be picked if the class is also annotated with@RequestMappingannotation. But that’s a different scenario.@Servicebeans hold the business logic and call methods in the repository layer.What’s special about @Service?
Apart from the fact that it’s used to indicate, that it’s holding the business logic, there’s nothing else noticeable in this annotation; but who knows, Spring may add some additional exceptional in future.
Similar to above, in the future Spring may add special functionalities for
See less@Service,@Controllerand@Repositorybased on their layering conventions. Hence, it’s always a good idea to respect the convention and use it in line with layers.