About Spring @Component, @Repository, @Service and @Controller Annotations.

Spring @Component, @Service, @Repository and @Controller annotations are used for automatic bean detection using classpath scan in Spring framework.
These four  annotations are same logically.

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

@Component
 This is a general-purpose stereotype annotation(auto scan) indicating that the class is a spring component. @Component annotation picks them up and registers their following classes as beans, just as if they were annotated with @Component.

@Repository

This is special type of @Component annotation. 
This is to indicate that the class defines a data repository. 

@Controller

This is special type of @Component annotation.
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?
We 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. We can only use @RequestMapping on @Controller annotated classes.
  
@Service
This is special type of @Component annotation. 
@Services hold business logic and call method in repository layer.

Summary:

| 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)      |



Comments

Popular posts from this blog

How to find the companyId in Liferay.

How to add portlet in liferay theme.

How to create soap client for ofbiz service or ofbiz service invocation from another application.