1. Introduction
1.绪论
In this quick tutorial, we’ll learn about the differences between @ComponentScan and @EnableAutoConfiguration annotations in the Spring Framework.
在这个快速教程中,我们将了解Spring框架中@ComponentScan 和@EnableAutoConfiguration 注解之间的区别。
2. Spring Annotations
2.Spring注解
Annotations make it easier to configure the dependency injection in Spring. Instead of using XML configuration files, we can use Spring Bean annotations on classes and methods to define beans. After that, the Spring IoC container configures and manages the beans.
注解使配置Spring中的依赖注入变得更加容易。我们可以使用类和方法上的Spring Bean注解来定义Bean,而不是使用XML配置文件。之后,Spring IoC容器会配置和管理这些Bean。
Here’s an overview of the annotations that we are going to discuss in this article:
下面是我们将在本文中讨论的注释的概述。
- @ComponentScan scans for annotated Spring components
- @EnableAutoConfiguration is used to enable the auto-configuration
Let’s now look into the difference between these two annotations.
现在让我们来看看这两个注释的区别。
3. How They Differ
3.它们有什么不同
The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.
这些注解的主要区别在于:@ComponentScan扫描Spring组件,而@EnableAutoConfiguration用于自动配置Spring Boot应用程序中classpath中存在的bean。
Now, let’s go through them in more detail.
现在,让我们更详细地了解它们。
3.1. @ComponentScan
3.1. @ComponentScan
While developing an application, we need to tell the Spring framework to look for Spring-managed components. @ComponentScan enables Spring to scan for things like configurations, controllers, services, and other components we define.
在开发应用程序时,我们需要告诉Spring框架去寻找Spring管理的组件。@ComponentScan使Spring能够扫描配置、控制器、服务和其他我们定义的组件等东西。
In particular, the @ComponentScan annotation is used with @Configuration annotation to specify the package for Spring to scan for components:
特别是,@ComponentScan注解与@Configuration注解一起使用,以指定Spring用于扫描组件的包。
@Configuration
@ComponentScan
public class EmployeeApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args);
// ...
}
}
Alternatively, Spring can also start scanning from the specified package, which we can define using basePackageClasses() or basePackages(). If no package is specified, then it considers the package of the class declaring the @ComponentScan annotation as the starting package:
另外,Spring也可以从指定的包开始扫描,我们可以使用basePackageClasses()或basePackages()定义该包。如果没有指定包,那么它将把声明@ComponentScan注解的类的包作为起始包:。
package com.baeldung.annotations.componentscanautoconfigure;
// ...
@Configuration
@ComponentScan(basePackages = {"com.baeldung.annotations.componentscanautoconfigure.healthcare",
"com.baeldung.annotations.componentscanautoconfigure.employee"},
basePackageClasses = Teacher.class)
public class EmployeeApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args);
// ...
}
}
In the example, Spring will scan the healthcare and employee packages and the Teacher class for components.
在这个例子中,Spring将扫描healthcare和employee包以及Teacher类中的组件。
Spring searches the specified packages along with all its sub-packages for classes annotated with @Configuration. Additionally, the Configuration classes can contain @Bean annotations, which register the methods as beans in the Spring application context. After that, the @ComponentScan annotation can auto-detect such beans:
Spring在指定的包及其所有子包中搜索带有@Configuration注解的类。另外,Configuration类可以包含@Bean注解,它将方法注册为Spring应用上下文中的bean。之后,@ComponentScan注解可以自动检测此类Bean。
@Configuration
public class Hospital {
@Bean
public Doctor getDoctor() {
return new Doctor();
}
}
Furthermore, the @ComponentScan annotation can also scan, detect, and register beans for classes annotated with @Component, @Controller, @Service, and @Repository.
此外,@ComponentScan 注解还可以扫描、检测和注册用@Component、@Controller、@Service和@Repository注解的类的bean。
For example, we can create an Employee class as a component which can be scanned by the @ComponentScan annotation:
例如,我们可以创建一个Employee类作为组件,它可以被@ComponentScan注解扫描。
@Component("employee")
public class Employee {
// ...
}
3.2. @EnableAutoConfiguration
3.2.@EnableAutoConfiguration
The @EnableAutoConfiguration annotation enables Spring Boot to auto-configure the application context. Therefore, it automatically creates and registers beans based on both the included jar files in the classpath and the beans defined by us.
@EnableAutoConfiguration annotation使Spring Boot能够自动配置应用程序上下文。因此,它将根据classpath中包含的jar文件和我们定义的Bean自动创建和注册Bean。
For example, when we define the spring-boot-starter-web dependency in our classpath, Spring boot auto-configures Tomcat and Spring MVC. However, this auto-configuration has less precedence in case we define our own configurations.
例如,当我们在classpath中定义spring-boot-starter-web依赖项时,Spring boot会自动配置Tomcat和Spring MVC。然而,如果我们定义自己的配置,这种自动配置的优先级就会降低。
The package of the class declaring the @EnableAutoConfiguration annotation is considered as the default. Therefore, we should always apply the @EnableAutoConfiguration annotation in the root package so that every sub-packages and class can be examined:
声明了@EnableAutoConfiguration注解的类的包被认为是默认的。因此,我们应该始终在根包中应用@EnableAutoConfiguration注解,这样就可以检查每个子包和类。
@Configuration
@EnableAutoConfiguration
public class EmployeeApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args);
// ...
}
}
Furthermore, the @EnableAutoConfiguration annotation provides two parameters to manually exclude any parameter:
此外,@EnableAutoConfiguration注解提供了两个参数来手动排除任何参数。
We can use exclude to disable a list of classes that we do not want to be auto-configured:
我们可以使用exclude来禁用我们不希望被自动配置的类的列表。
@Configuration
@EnableAutoConfiguration(exclude={JdbcTemplateAutoConfiguration.class})
public class EmployeeApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args);
// ...
}
}
We can use excludeName to define a fully qualified list of class names that we want to exclude from the auto-configuration:
我们可以使用excludeName来定义一个我们想从自动配置中排除的完全合格的类名列表。
@Configuration
@EnableAutoConfiguration(excludeName = {"org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration"})
public class EmployeeApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args);
// ...
}
}
Since Spring Boot 1.2.0, we can use the @SpringBootApplication annotation, which is a combination of the three annotations @Configuration, @EnableAutoConfiguration, and@ComponentScan with their default attributes:
自Spring Boot 1.2.0以来,我们可以使用@SpringBootApplication注解,它是三个注解@Configuration、@EnableAutoConfiguration和@ComponentScan的组合,并带有其默认属性。
@SpringBootApplication
public class EmployeeApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args);
// ...
}
}
4. Conclusion
4.总结
In this article, we learned about the differences between @ComponentScan and @EnableAutoConfiguration in Spring Boot.
在这篇文章中,我们了解了Spring Boot中@ComponentScan和@EnableAutoConfiguration之间的区别。
As always, the code for these examples is available over on GitHub.
像往常一样,这些例子的代码可以在GitHub上找到over。