问题

Spring可以交换使用@ Component@ Repository@ Service注释,还是除了充当符号设备外,还可以提供任何特定的功能吗?

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

或者注释是否也会影响课堂的行为和功能?


#1 热门回答(1066 赞)

FromSpring Documentation

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

#2 热门回答(394 赞)

由于许多答案已经说明了这些注释的用途,我们在这里将重点讨论它们之间的一些细微差别。

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

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

@Component

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

@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后置处理器将一个顾问程序添加到用@ @ Repository注释的bean中,以便捕获任何特定于平台的异常,然后重新将其作为Spring未经检查的数据访问异常之一。

@Controller

“@ Controller”注释表示特定的类用作控制器的角色。 “@ Controller”注释充当注释类的刻板印象,表明其作用。

@Controller有什么特别之处?

尽管它们看起来相同,但我们无法像使用@ Service@ Repository那样切换这个注释。调度器扫描用“@ Controller”注解的类并检测其中的@ RequestMapping注解。我们只能在@ Controller注释类中使用@ RequestMapping

@服务

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

@Service有什么特别之处?

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

还有什么?

与上面类似,未来Spring可能会根据分层约定为“@ Service”,“@ Controller”和“@ Repository”添加特殊功能。因此,尊重公约并将其与层次结合使用始终是一个好主意。


#3 热门回答(383 赞)

它们几乎相同 - 所有这些都意味着该类是一个Spring bean。 @Service@ Repository@Controller是专门的@Component。您可以选择执行特定的操作。例如:

  • @Controller bean被spring-mvc使用
  • @Repository bean有资格进行持久性异常转换

另一件事是你将这些组件语义地指定给不同的层。

“@ Component”提供的一件事是您可以使用它注释其他注释,然后以与@ @ Service相同的方式使用它们。

例如最近我做了:

@Component
@Scope("prototype")
public @interface ScheduledJob {..}

因此,所有使用@ ScheduledJob注解的类都是spring bean,除此之外,它们还被注册为quartz作业。您只需提供处理特定注释的代码。


原文链接