Spring @Component,@ Service,@ Repository,@ Controller的差异

 Spring @Component,@ Service,@ Repository,@ Controller差异


Spring @Component,@ Service,@ Repository和@Controller注释用于在Spring框架中扫描指定路径的类注册为bean。

@Component是一个通用注释。
@ Service,@ Repository,@ Controller与@Component的区别在于它们是@Component的特例,用于特定目的。区别仅在于分类。

对于所有这些注释(刻板印象),从技术上讲,核心目的是相同的。
Spring自动扫描并识别所有使用“ @ Component,@ Service,@ Repository,@ Controller ” 注释的类,并可以使用ApplicationContext获取这些bean。

对于所有@Component,@ Service,@ Repository和@Controller原型组件,都是根据BeanNameGenerator策略分配bean名称。即类名首字母小写  。

我们还可以在注释期间提供我们的名称选择,这将是高优先级

他们的区别就好像让你在下图中找不同点

我是找不出来的。如果谁知道他们的不同请博客下留言

2018 8 15 再次编辑 时隔大半年了,

下面的话是春天告诉我的

在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)      │
└────────────┴─────────────────────────────────────────────────────┘

英文好的看这个

https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-stereotype-annotations

 

 

再次编辑下

@ 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注释注释。如果我们定义自己的自定义注释并使用它进行注释@Component,那么它也将被扫描<context:component-scan>


@Repository

这是为了表明该类定义了一个数据存储库。

@Repository有什么特别之处?

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

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

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


@Controller

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

@Controller有什么特别之处?

我们不能与任何其他关掉这个注解像@Service或者@Repository,即使他们看起来一样。调度程序扫描带注释的类@Controller并检测其中的@RequestMapping注释。我们只能在带@Controller注释的类上使用@RequestMapping


@服务

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

@Service有什么特别之处?

除了它用于表明它持有业务逻辑这一事实之外,这个注释没有明显的特点,但是谁知道,spring可能在未来增加一些额外的特殊功能。


还有什么?

与上述类似,在未来的春天可以选择添加特殊功能的@Service@Controller@Repository根据他们的分层约定。

因此,尊重惯例并将其与层一致使用始终是一个好主意。

猜你喜欢

转载自blog.csdn.net/uotail/article/details/81806195