Difference between @Component, @Repository & @Service annotations in Spring?

With respect to scan-auto-detection and dependency injection for Bean Definition in spring all of these annotations @Component, @Service, @Repository and @Controller are same. We can use one in the place of another and still it will be registered as a bean. But the bean can loose some functionalities, that we will discuss in later part of the post.

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

@Component : This is a general-purpose stereotype annotation indicating that the class is spring component. You must have seen the following entry in your spring configuration file:

<context:component-scan> 

This means spring will scan for @Component  only and do not looks for @Service, @Repository and @Controller in general. 

But then how @Service, @Repository and @Controller are get registered by component-scan . As They themselves are annotated with @Component. Just take a look at @Service, @Repository and @Controller annotation definition:

@Component
public @interface Service {
    ….
}
 
@Component
public @interface Repository {
    ….
}
 
@Component
public @interface Controller {
    …
}

Thus it’s not wrong to say that @Service, @Repository and @Controller 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.

In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role of a Data Access Object or DAO.Spring 2.5 introduces further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Service, @Repository and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

Now let us check why spring have created these different types of annotations if all are same as a @Component

@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  annotations @RequestMapping  within them. You can only use @RequestMapping on @Controller classes.

@Service : @Service 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 Spring can add any special functionalities to this annotation in near future. 

So all annotations have one common feature which is registering the class as a bean. And some have some special feature (as @Controller and @Repository) which we have already discussed. In future Spring may choose to add special functionalities for @Service, @Controller and @Repository based on their layering representation. Hence its always a good practice to follow the conventions and use the correct at the specific layer. Following is the layering representation of these annotations.

Annotation Meaning                                            
@Component  generic stereotype for any Spring-managed component
@Repository  stereotype for persistence layer
@Service  stereotype for service layer
@Controller  stereotype for presentation layer (spring-mvc)

spring 12

FOLLOW US ON LinkedIn



Explore Tutu'rself