编程语言
首页 > 编程语言> > java – 我什么时候应该使用@Component而不是@Service?

java – 我什么时候应该使用@Component而不是@Service?

作者:互联网

我已经阅读过What’s the difference between @Component, @Repository & @Service annotations in Spring?中的每个Spring注释

我知道@Component和@Service在Spring Framework中几乎一样.差异与它的用法有关,所以我只是将@Service用于我访问我的存储库的服务层.

我想知道选择@Component而不是@Service的更好方法是什么.请注意,我现在正在为我创建的工厂使用@Component,因为工厂不需要处理存储库.

解决方法:

请记住,这些是Spring的刻板印象,应该应用于通常应用围绕一个代码的刻板印象或假设.

>如果您的组件是通用组件,并非真正位于服务层,或者可访问但可以保持状态,则使用@Component.
>如果您的组件是特定服务,位于服务层,或者可访问且本身不具有状态,请使用@Service.

Spring将这两者最终视为@Components.

标签:java,spring,spring-boot,software-quality
来源: https://codeday.me/bug/20190828/1748874.html