【发布时间】:2019-10-09 03:15:48
【问题描述】:
我了解@Component 和@Controller、@Component 和@Repository 之间的区别,但找不到与@Component 相比我们在@Service 中获得的额外功能。
【问题讨论】:
标签: java spring spring-boot spring-mvc annotations
我了解@Component 和@Controller、@Component 和@Repository 之间的区别,但找不到与@Component 相比我们在@Service 中获得的额外功能。
【问题讨论】:
标签: java spring spring-boot spring-mvc annotations
我们可以对每个 bean 直接使用@Component,但为了更好地理解和维护大型应用程序,我们使用@Controller, @Service, @Repository。
@Component: generic stereotype for any Spring-managed component
@Service: stereotype for service layer
@Component
@Controller、@Service 和@Repository 注释的定义告诉@Service 是@Component 的一种特殊类型。特殊类型的注解也会被扫描,因为它们本身带有@Component注解,这意味着它们也是@Components。如果我们定义自己的自定义注解并使用@Component 进行注解,它也会被<context:component-scan> 扫描
@Component
public @interface Service {
….
}
@Component
public @interface Repository {
….
}
@Component
public @interface Controller {
…
}
@Service
@Service bean 在存储库层中保存业务逻辑和调用方法。
【讨论】: