@Service vs @Component在Spring中的区别
@Service vs @Component在Spring中的区别
这个问题已经有了答案:
我理解@Component和@Controller、@Component和@Repository之间的区别,但是无法找到在@Service中相对于@Component有什么额外的特性。
admin 更改状态以发布 2023年5月22日
我们可以直接为每个bean使用@Component
,但为了更好地理解和维护大型应用程序,我们使用@Controller、@Service、@Repository
。
@Component: generic stereotype for any Spring-managed component @Service: stereotype for service layer
@Component
@Controller、@Service和@Repository注解的定义表明@Service是@Compoent的一种特殊类型。特殊类型的注解也会被扫描,因为它们本身被注解了@Component
注解,这意味着它们也是@Component
。如果我们定义自己的自定义注解并用@Component
进行注释,则它也将被
扫描。
@Component public @interface Service { …. } @Component public @interface Repository { …. } @Component public @interface Controller { … }
@Service
@Service
bean保存业务逻辑并调用存储库层中的方法。