@Component vs @Repository and @Service in Spring – Spring中的@Component与@Repository和@Service对比

最后修改: 2018年 7月 15日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this quick tutorial, we’re going to learn about the differences between the @Component, @Repository, and @Service annotations in the Spring Framework.

在这个快速教程中,我们将了解Spring框架中@Component、@Repository、@Service注解的区别。

2. Spring Annotations

2.Spring注解

In most typical applications, we have distinct layers like data access, presentation, service, business, etc.

在大多数典型的应用程序中,我们有不同的层,如数据访问、演示、服务、业务等。

Additionally, in each layer we have various beans. To detect these beans automatically, Spring uses classpath scanning annotations.

此外,在每一层中,我们有各种Bean。为了自动检测这些Bean,Spring使用classpath扫描注释

Then it registers each bean in the ApplicationContext.

然后它在ApplicationContext中注册每个Bean。

Here’s a quick overview of a few of these annotations:

下面是对其中几个注释的快速概述。

  • @Component is a generic stereotype for any Spring-managed component.
  • @Service annotates classes at the service layer.
  • @Repository annotates classes at the persistence layer, which will act as a database repository.

We already have an extended article about these annotations, so we’ll keep the focus here to the differences between them.

我们已经有一篇关于这些注释的扩展文章,所以我们在这里将重点放在它们之间的区别上。

3. What’s Different?

3.有什么不同?

The major difference between these stereotypes is that they are used for different classifications. When we annotate a class for auto-detection, we should use the respective stereotype.

这些刻板印象的主要区别在于它们被用于不同的分类。当我们为自动检测注释一个类时,我们应该使用相应的刻板印象。

Now let’s go through them in more detail.

现在让我们更详细地了解它们。

3.1. @Component

3.1.@Component

We can use @Component across the application to mark the beans as Spring’s managed components. Spring will only pick up and register beans with @Component, and doesn’t look for @Service and @Repository in general.

我们可以在整个应用中使用@Component来标记Bean为Spring的管理组件。Spring只会接收和注册带有@Component的Bean,而不会寻找一般的@Service@Repository

They are registered in ApplicationContext because they are annotated with @Component:

它们被注册在ApplicationContext中,因为它们被注释为@Component

@Component
public @interface Service {
}
@Component
public @interface Repository {
}

@Service and @Repository are special cases of @Component. They are technically the same, but we use them for the different purposes.

@Service@Repository@Component的特殊情况。它们在技术上是一样的,但我们将它们用于不同的目的。

3.2. @Repository

3.2.@Repository

@Repository’s job is to catch persistence-specific exceptions and re-throw them as one of Spring’s unified unchecked exceptions.

@Repository的工作是捕捉特定于持久性的异常,并将其作为Spring统一的未检查的异常之一重新抛出

For this, Spring provides PersistenceExceptionTranslationPostProcessor, which we are required to add in our application context (already included if we’re using Spring Boot):

为此,Spring提供了PersistenceExceptionTranslationPostProcessor,我们需要将其添加到我们的应用程序上下文中(如果我们使用Spring Boot,则已经包含在内)。

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

This bean post processor adds an advisor to any bean that’s annotated with @Repository.

这个Bean Post处理器为任何带有@Repository.注释的Bean添加一个顾问。

3.3. @Service

3.3.@服务

We mark beans with @Service to indicate that they’re holding the business logic. Besides being used in the service layer, there isn’t any other special use for this annotation.

我们用@Service来标记Bean,以表明它们持有的是业务逻辑。除了在服务层中使用,这个注解没有任何其他特殊用途。

4. Conclusion

4.结论

In this article, we learned about the differences between the @Component, @Repository, and @Service annotations. We examined each annotation separately to understand their areas of use.

在这篇文章中,我们了解了@Component、@Repository、@Service注解之间的区别。我们分别研究了每个注解,以了解它们的使用范围。

In conclusion, it’s always a good idea to choose the annotation based on their layer conventions.

总之,根据他们的图层惯例来选择注释总是一个好主意。