Spring @EntityScan vs. @ComponentScan – Spring @EntityScan vs. @ComponentScan

最后修改: 2021年 3月 4日

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

1. Introduction

1.绪论

When writing our Spring application we might need to specify a certain list of packages that contain our entity classes. Similarly, at some point, we would need only a specific list of our Spring beans to be initialized. This is where we can make use of @EntityScan or @ComponentScan annotations.

在编写我们的Spring应用程序时,我们可能需要指定包含我们实体类的某个包的列表。同样地,在某些时候,我们只需要我们的Spring Bean的特定列表被初始化。这时我们可以利用@EntityScan@ComponentScan注释。

To clarify the terms we use here, components are classes with @Controller, @Service, @Repository, @Component, @Bean, etc. annotations. Entities are classes marked with @Entity annotation.

为了澄清我们在这里使用的术语,组件是带有@Controller @Service@Repository@Component@Bean、等注释的类。实体是用@Entity注解标记的类。

In this short tutorial, we’ll discuss the usage of @EntityScan and @ComponentScan in Spring, explain what are they used for, and then point out their differences.

在这个简短的教程中,我们将讨论Spring中@EntityScan@ComponentScan的用法,解释它们的用途,然后指出它们的区别。

2. The @EntityScan Annotation

2.@EntityScan注释

When writing our Spring application we will usually have entity classes – those annotated with @Entity annotation. We can consider two approaches to placing our entity classes:

在编写Spring应用程序时,我们通常会有实体类–那些用@Entity注解的类。我们可以考虑两种方法来放置我们的实体类。

  • Under the application main package or its sub-packages
  • Use a completely different root package

In the first scenario, we could use @EnableAutoConfiguration to enable Spring to auto-configure the application context.

在第一种情况下,我们可以使用@EnableAutoConfiguration来使Spring自动配置应用程序上下文。

In the second scenario, we would provide our application with the information where these packages could be found. For this purpose, we would use @EntityScan.

在第二种情况下,我们将向我们的应用程序提供可以找到这些包的信息。为此目的,我们将使用@EntityScan.

@EntityScan annotation is used when entity classes are not placed in the main application package or its sub-packages. In this situation, we would declare the package or list of packages in the main configuration class within @EntityScan annotation. This will tell Spring where to find entities used in our application:

@EntityScan 注解是在实体类没有放在主应用程序包或其子包中时使用。在这种情况下,我们会在@EntityScan注解中的主配置类中声明包或包的列表。这将告诉Spring在哪里可以找到我们应用程序中使用的实体。

@Configuration
@EntityScan("com.baeldung.demopackage")
public class EntityScanDemo {
    // ...
}

We should be aware that using @EntityScan will disable Spring Boot auto-configuration scanning for entities.

我们应该注意,使用@EntityScan将禁用Spring Boot对实体的自动配置扫描。

3. @ComponentScan Annotation

3.@ComponentScan Annotation

Similar to @EntityScan and entities, if we want Spring to use only a specific set of bean classes, we would use @ComponentScan annotation. It’ll point to the specific location of bean classes we would want Spring to initialize.

@EntityScan和实体类似,如果我们希望Spring只使用一组特定的bean类,我们将使用@ComponentScan注解。它将指向我们希望Spring初始化的bean类的特定位置

This annotation could be used with or without parameters. Without parameters, Spring will scan the current package and its sub-packages, while, when parameterized, it’ll tell Spring where exactly to search for packages.

这个注解可以有参数或无参数地使用。没有参数,Spring会扫描当前包和它的子包,而当参数化时,它会告诉Spring到底在哪里搜索包。

Concerning parameters, we can provide a list of packages to be scanned (using basePackages parameter) or we can name specific classes where packages they belong to will also be scanned (using basePackageClasses parameter).

关于参数,我们可以提供一个要扫描的包的列表(使用basePackages参数),或者我们可以命名特定的类,它们所属的包也将被扫描(使用basePackageClasses参数)。

Let’s see an example of @ComponentScan annotation usage:

让我们看看@ComponentScan注解的使用实例。

@Configuration
@ComponentScan(
  basePackages = {"com.baeldung.demopackage"}, 
  basePackageClasses = DemoBean.class)
public class ComponentScanExample {
    // ...
}

4. @EntityScan vs. @ComponentScan

4.@EntityScan@ComponentScan对比

In the end, we can say that these two annotations are intended for completely different purposes.

最后,我们可以说,这两个注释的目的完全不同。

Their similarity is that they both contribute to our Spring application configuration. @EntityScan should specify which packages do we want to scan for entity classes. On the other hand, @ComponentScan is a choice when specifying which packages should be scanned for Spring beans.

它们的相似之处在于,它们都有助于我们的Spring应用配置。@EntityScan应该指定我们要为实体类扫描哪些包。另一方面,@ComponentScan是在指定扫描Spring Bean的包时的一种选择。

5. Conclusion

5.总结

In this short tutorial, we discussed the usage of @EntityScan and @ComponentScan annotations and also pointed to their differences.

在这个简短的教程中,我们讨论了@EntityScan@ComponentScan注解的用法,还指出了它们的区别。