小编典典

Spring中的@ Component,@ Repository和@Service批注有什么区别?

spring

@Component,@Repository@Service注释可以在Spring中互换使用吗,或者除了用作注释设备之外,它们还提供任何特定功能吗?

换句话说,如果我有一个Service类,并且将注释从更改@Service@Component,它的行为是否仍将相同?

还是注释也会影响类的行为和功能?


阅读 1907

收藏
2020-04-11

共2个答案

小编典典

从Spring文档:

在Spring 2.0和更高版本中,@Repository注释是满足存储库角色或构造型(也称为数据访问对象或DAO)的任何类的标记。该标记的用途包括自动翻译例外。

Spring 2.5中引入了进一步典型化注解:@Component, @Service,和@Controller。@Component是任何Spring托管组件的通用构造型。@Repository,@Service和@Controller分别是@Component针对特定用例的专业化,例如分别在持久性,服务和表示层。

因此,你可以用你的注解组件类@Component,但如果用注解它们@Repository@Service或者@Controller ,你的类能更好地适合于通过工具处理,或与切面进行关联。例如,这些构造型注释成为切入点的理想目标。

因此,如果你在使用@Component还是@Service在服务层之间进行选择,@Service显然是更好的选择。同样,如上所述,@Repository在持久层中已经支持作为自动异常转换的标记。

┌────────────┬─────────────────────────────────────────────────────┐
│ 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)      │
└────────────┴─────────────────────────────────────────────────────┘
2020-04-11
小编典典

由于许多答案已经说明了这些批注的用途,因此我们将重点关注它们之间的一些细微差异。

首先相似

值得再次强调的第一点是,对于BeanDefinition的扫描自动检测和依赖项注入,所有这些注释(即@ Component,@ Service,@ Repository,@ Controller)都是相同的。我们可以用一个代替另一个,并且仍然可以解决问题。

@ Component,@ Repository,@ Controller和@Service之间的区别

@Component

这是一个通用的构造型注释,指示该类是spring组件。

有什么特别的@Component

<context:component-scan>只扫描@Component和不查找@Controller@Service@Repository一般。之所以扫描它们,是因为它们本身带有注释@Component

只要看一看@Controller@Service@Repository注释的定义:

@Component
public @interface Service {
    ….
}
@Component
public @interface Repository {
    ….
}
@Component
public @interface Controller {
    …
}

因此,这不是错误的说法@Controller@Service并且@Repository是特殊类型的@Component注释。<context:component-scan>拾取它们并将其后继类注册为bean,就像它们使用注释一样@Component

特殊类型的注释也会被扫描,因为它们本身会被@Component注释,这意味着它们也是@Components。如果我们定义自己的自定义批注并使用添加注释@Component,则也会使用对其进行扫描<context:component-scan>

@资料库

这表示该类定义了一个数据存储库。

@Repository有什么特别之处?

除了指出这是基于AnnotationConfiguration之外,它@Repository的工作是捕获特定于平台的异常,并将其作为Spring统一的未经检查的异常之一重新抛出。为此,我们提供了PersistenceExceptionTranslationPostProcessor,我们需要将其添加到Spring的应用程序上下文中,如下所示:

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

这个bean后处理器将顾问程序添加到任何带有注解的bean,@Repository以便捕获任何特定于平台的异常,然后将其重新抛出为Spring的未经检查的数据访问异常之一。

@Controller

@Controller注解表明特定类供应控制器的作用。该@Controller注释充当注解类刻板印象,这表明它的作用。

@Controller有什么特别之处?

即使它们看起来相同,我们也无法将其与任何其他类似@Service或切换@Repository。调度程序将扫描@Controller带有@RequestMapping注释的类,并检测其中带有注释的方法。我们可以使用@RequestMapping上/只,其类注释与方法@Controller,这将不与工作@Component,@Service,@Repository等…

注意:如果某个类已经通过任何其他方法(例如通过@Bean或通过@Component@Service等等…注释)注册为Bean ,则@RequestMapping如果该类也已使用注释进行@RequestMapping注释,则可以选择该类。但这是另一种情况。

@Service

@Service bean在存储库层中保存业务逻辑和调用方法。

@Service有什么特别之处?

除了它用来指示它保持业务逻辑这一事实外,此注释中没有其他值得注意的东西。但是谁知道,Spring将来可能会增加一些额外的例外。

还有什么?

与上述类似,在Spring将来可能会增加对特殊功能@Service,@Controller@Repository根据他们的分层约定。因此,遵守约定并与层配合使用始终是一个好主意。

2020-04-11