Spring @Component,@Service,@Repository, @Controlle

1.@Component

@Component is used at the class level to make the class a component. These classes are auto-detected through classpath scanning. In java configuration, @ComponentScan is used for auto-detection of components, and in spring application context XML, component-scan tag is used for auto-detection via classpath. @Component has a value attribute which is a component name which will also be treated as spring bean name.

2.@Service

@Service is used for service class annotations. Service classes can act as the business service facade of the j2EE pattern. Service classes implement business logic using DAOs, entity classes, etc. @Service is automatically detected via classpath scanning. Annotating classes with @Service gives the logical sense that these classes are services. If we use @Component in the service class instead of @Service there will be no problem, but for better readability, the service class should be annotated with @Service. @Service can be understood as a special case of @Component.

3.@Repository

@Repository is used for persistence layer annotation and these types of classes should be annotated with @Repository annotation for auto-detection via classpath scanning. DAO classes should be annotated with @Repository annotation for auto-detection. @Repository can be understood as a special case of @Component.

4.@Controller

@Controller is used at the class level of spring MVC. It indicates that the class is a web controller. These annotated classes are automatically detected by @Controller via classpath scanning. The @Controller annotation is often used in conjunction with the @RequestMapping annotation in spring MVC.

Guess you like

Origin blog.csdn.net/lssqk/article/details/103496167