Spring Framework: @Component, @Service, @Repository and @Controller

In order to continue with this article, you should have a proper understanding of Spring component scanning feature.  if you don’t know about it, please go through Component Scanning in Spring article before continue with this one.

 

@Component, @Repository, @Service and @Controller annotations

Spring @Component, @Service, @Repository and @Controller annotations are used for automatic bean detection/discovery using classpath scanning (@ComponentScan) in Spring framework. Those annotation will indicate the component scanner that those beans are spring managed beans. These annotations are called Stereotype annotations as well.

@Component is a generic annotation/stereotype and can be applied to any class within the project to make it as spring managed component. Then it will be automatically detected during the Spring classpath scanning. Difference of @Service, @Repository, @Controller with @Component is that they are special cases of @Component and used for particular purposes.

E.g:- spring managed components in the persistence, service, and presentation layers.

For all these annotations (stereotypes), technically the core purpose is same. Spring automatically identifies all these classes that are annotated with  @Component, @Service, @Repository, @Controller  during classpath scanning and registers bean in ApplicationContext.

Spring IOC DI (1)

@Component

This is a generic annotation and can be applied to any class of the application to make it a spring managed component(simply, generic stereotype for any spring managed component). when the classpath is scanned by the spring’s component-scan (@ComponentScan) feature, it will identify the classes annotated with @Component annotation (within the given package) and create the beans of such classes and register them in the ApplicationContext.

 

@Repository

This annotation designated the bean class as a spring managed component in the persistence layer/DAO layer. It notify the spring that the class will contain the logic for accessing the data. @Repository translates any unchecked exceptions (application specific exceptions, database-specific SQL exceptions etc…) thrown from DAO methods into Spring’s DataAccessException. (once the exception is occurred, it will communicate with PersistenceExceptionTranslationPostProcessor to translate the exception into Spring’s DataAccessException)

 

@Service

This annotation designated the bean class as a spring managed component in the business layer. It notify the spring that the class contains the business logic.

@Service is a specialization of @Component, which indicates that the annotated class is a business layer class. 

It doesn’t currently provide any additional behavior over the @Component annotation. This is introduced to in the spring framework to add any specific features to the service classes in the future.

 

@Controller

This annotations designates the bean class as the controller in the presentation layer (Spring MVC Controller).When you add the @Controller annotation to a class, you can use another annotation i.e @RequestMapping; to map URLs to instance methods of a class.This annotation is widely used in Spring MVC applications.

 

 

One thought on “Spring Framework: @Component, @Service, @Repository and @Controller

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s