Pages

Difference Between @Component, @Service and @Repository


http://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in

As many of the answers here states what these annotations are used for, I’m going to point out some minor differences between them.
First the Similarity
First point worth highlighting again is that with respect to scan-auto-detection and dependency injection for BeanDefinition all these annotation (viz., @Component, @Service, @Repository, @Controller) are all the same. You can use one in place of another and can still get your way around.

Differences between @Component, @Repository, @Controller and @Service

@Component
This is a general-purpose stereotype annotation indicating that the class is spring component.
What’s special about @Component
<context:component-scan> only scans @Component and do not looks for @Controller@Service and @Repository in general. They are scanned because they themselves are annotated with @Component.
Just take a look at @Controller@Service and @Repository annotation definition
@Component
public @interface Service {
    ….
}

@Component
public @interface Repository {
    ….
}

@Component
public @interface Controller {
    
}
Thus it’s not wrong to say that @Controller@Service and @Repository are special type of @Component annotation. <context:component-scan> picks them up and registers their following classes as beans, just as if they were annotated with @Component.
They are scanned because they themselves are annotated with @Component annotation. If you define your own custom annotation and annotate it with @Component, then it will also get scanned with <context:component-scan>

@Repository
This is to indicate that the class defines a data repository.
What’s special about @Repository?
In addition to point 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. And for this, we’re provided with PersistenceExceptionTranslationPostProcessor, that we’re required to add in our Spring’s application context like this:
<bean class=”org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor”/>
This bean post processor adds an advisor to any bean that’s annotated with @Repository so that any platform specific exceptions are caught and then rethrown as one of Spring’s unchecked data access exceptions.

@Controller
The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role.
What’s special about @Controller?
You cannot switch this annotation with any other like @Service or @Repository, even though they look same. The dispatcher scans the classes annotated with @Controller and detects @RequestMapping annotations within them. You can only use @RequestMapping on @Controller annotated classes.

@Service
@Services hold business logic and call method in repository layer.
What’s special about @Service?
Apart from the fact that it is used to indicate that it's holding the business logic, there’s no noticeable specialty that this annotation provides, but who knows, spring may add some additional exceptional in future.

What else?
Similar to above, in future Spring may choose to add special functionalities for @Service@Controller and @Repository based on their layering conventions. Hence its always a good idea to respect the convention and use them in line with layers.