什么是@Component,@Repository和放大器之间的差异; @Service注解春天? [英] What's the difference between @Component, @Repository & @Service annotations in Spring?

查看:542
本文介绍了什么是@Component,@Repository和放大器之间的差异; @Service注解春天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否 @Component @Repository &安培; @Service 注释可以在Spring互换使用或他们提供任何特殊的功能,除了作为一个符号设备?

在换句话说,如果我有一个服务类,并更改从 @Service 注释,以 @Component ,将仍然行为相同的方式?

抑或是注释也影响类的行为和功能?


解决方案

从<一个href=\"http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/beans.html#beans-stereotype-annotations\">Spring文档:


  

在Spring 2.0中和以后,@Repository注解是一个标记
  一个符合角色或原型任何类(也称为数据
  存储库的访问对象或DAO)。在这个标记的使用
  是异常的自动转换。


  
  

春2.5引入进一步典型化注解:@Component,
  @Service和@Controller。 @Component为任何一个通用的刻板印象
  Spring管理的组成部分。 @Repository,@Service和@Controller是
  @Component的细化更具体的使用情况下,
  例如,在持久性,服务和presentation层,
  分别。


  
  

因此​​,您可以用@Component来注解你的组件类,
  但如果用@Repository,@Service或@Controller来注解它们
  相反,你的类能更好地被工具处理,
  或者与切面进行关联。例如,这些典型化注解
  让理想目标的切入点。


  
  

因此​​,如果您使用的是@Component还是@Service对之间进行选择
  你的服务层,@Service显然是更好的选择。同样的,
  如上所述,@Repository已经作为一个标记
  自动异常翻译持久层。


  |注释|含义|
+ ------------ + ------------ ----------------- +
| @Component |通用形式任何Spring管理组件|
| @Repository |刻板印象的持久层|
| @服务|刻板印象为服务层|
| @Controller |刻板印象为presentation层(弹簧MVC)|

Can @Component, @Repository & @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?

In other words, if I have a Service class and I change the annotation from @Service to @Component, will it still behave the same way?

Or does the annotation also influence the behavior and functionality of the class?

解决方案

From Spring Documentation:

In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.

Spring 2.5 introduces further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

| 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)      |

这篇关于什么是@Component,@Repository和放大器之间的差异; @Service注解春天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆