Java Spring Boot中@ComponentScan和@EnableAutoConfiguration有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35005158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:13:34  来源:igfitidea点击:

What is the difference between @ComponentScan and @EnableAutoConfiguration in Spring Boot?

javaspringspring-mvcspring-boot

提问by e2rabi

What is the difference between the @ComponentScanand @EnableAutoConfigurationannotations in Spring Boot? Is it necessary to add these? My application works very well without these annotations. I just want to understand why we have to add them.

Spring Boot 中的注解@ComponentScan@EnableAutoConfiguration注解有什么区别?有必要添加这些吗?没有这些注释,我的应用程序运行良好。我只是想了解为什么我们必须添加它们。

采纳答案by Shaheer

What is the difference between the @ComponentScan and @EnableAutoConfiguration annotations in Spring Boot?

Spring Boot 中的 @ComponentScan 和 @EnableAutoConfiguration 注解有什么区别?

@EnableAutoConfigurationannotation tells Spring Boot to "guess" how you will want to configure Spring, based on the jar dependencies that you have added. For example, If HSQLDB is on your classpath, and you have not manually configured any database connection beans, then Spring will auto-configure an in-memory database.

@EnableAutoConfiguration注释告诉 Spring Boot 根据您添加的 jar 依赖项“猜测”您将如何配置 Spring。例如,如果 HSQLDB 在您的类路径上,并且您没有手动配置任何数据库连接 bean,那么 Spring 将自动配置一个内存数据库。

@ComponentScantells Spring to look for other components, configurations, and services in the specified package. Spring is able to auto scan, detect and register your beans or components from pre-defined project package. If no package is specified current class package is taken as the root package.

@ComponentScan告诉 Spring 在指定的包中查找其他组件、配置和服务。Spring 能够从预定义的项目包中自动扫描、检测和注册您的 bean 或组件。如果未指定包,则将当前类包作为根包。

Is it necessary to add these?

有必要添加这些吗?

If you need Spring boot to Auto configure every thing for you @EnableAutoConfigurationis required. You don't need to add it manually, spring will add it internally for you based on the annotation you provide.

如果您需要 Spring Boot 来自动配置您需要的所有东西@EnableAutoConfiguration。不需要手动添加,spring会根据你提供的注解在内部为你添加。

Actually the @SpringBootApplicationannotation is equivalent to using @Configuration, @EnableAutoConfigurationand @ComponentScanwith their default attributes.

实际上,@SpringBootApplication注释相当于使用@Configuration,@EnableAutoConfiguration@ComponentScan使用它们的默认属性。

See also:

也可以看看:

回答by ShayneR

One of the main advantages of Spring Boot is its annotation driven versus traditional xml based configurations, @EnableAutoConfigurationautomatically configures the Spring application based on its included jar files, it sets up defaults or helper based on dependencies in pom.xml. Auto-configuration is usually applied based on the classpath and the defined beans. Therefore, we donot need to define any of the DataSource, EntityManagerFactory, TransactionManager etc and magically based on the classpath, Spring Boot automatically creates proper beans and registers them for us. For example when there is a tomcat-embedded.jar on your classpath you likely need a TomcatEmbeddedServletContainerFactory (unless you have defined your own EmbeddedServletContainerFactory bean). @EnableAutoConfiguration has a exclude attribute to disable an auto-configuration explicitly otherwise we can simply exclude it from the pom.xml, for example if we donot want Spring to configure the tomcat then exclude spring-bootstarter-tomcat from spring-boot-starter-web.

Spring Boot 的主要优势之一是它的注解驱动与传统的基于 xml 的配置相比,@EnableAutoConfiguration根据包含的 jar 文件自动配置 Spring 应用程序,它根据 pom.xml 中的依赖项设置默认值或帮助程序。自动配置通常基于类路径和定义的 bean 应用。因此,我们不需要定义任何 DataSource、EntityManagerFactory、TransactionManager 等,并且神奇地基于类路径,Spring Boot 会自动创建合适的 bean 并为我们注册它们。例如,当您的类路径中有一个 tomcat-embedded.jar 时,您可能需要一个 TomcatEmbeddedServletContainerFactory(除非您定义了自己的 EmbeddedServletContainerFactory bean)。@EnableAutoConfiguration 有一个 exclude 属性来显式禁用自动配置,否则我们可以简单地从 pom.xml 中排除它,

@ComponentScanprovides scope for spring component scan, it simply goes though the provided base packageand picks up dependencies required by @Bean or @Autowired etc, In a typical Spring application, @ComponentScan is used in a configuration classes, the ones annotated with @Configuration. Configuration classes contains methods annotated with @Bean. These @Bean annotated methods generate beans managed by Spring container. Those beans will be auto-detected by @ComponentScan annotation. There are some annotations which make beans auto-detectable like @Repository , @Service, @Controller, @Configuration, @Component. In below code Spring starts scanning from the package including BeanA class.

@ComponentScan为 spring 组件扫描提供了范围,它只是通过提供的基础包并选择 @Bean 或 @Autowired 等所需的依赖项,在典型的 Spring 应用程序中,@ComponentScan 用于配置类中,用 @ 注释的那些配置。配置类包含用@Bean 注释的方法。这些@Bean 注释的方法生成由 Spring 容器管理的 bean。这些 bean 将由 @ComponentScan 注释自动检测。有一些注释可以使 bean 自动检测,例如 @Repository 、@Service、@Controller、@Configuration、@Component。在下面的代码中,Spring 从包含 BeanA 类的包开始扫描。

@Configuration
@ComponentScan(basePackageClasses = BeanA.class)
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Config {

  @Bean
  public BeanA beanA(){
    return new BeanA();
  }

  @Bean
  public BeanB beanB{
    return new BeanB();
  }

}

回答by Hetal Rachh

@EnableAutoConfigurationin spring boot tells how you want to configure spring, based on the jars that you have added in your classpath. For example, if you add spring-boot-starter-webdependency in your classpath, it automatically configures Tomcat and Spring MVC.

@EnableAutoConfigurationin spring boot 根据您在类路径中添加的 jars 告诉您要如何配置 spring。例如,如果您spring-boot-starter-web在类路径中添加依赖项,它会自动配置 Tomcat 和 Spring MVC。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

You can use @EnableAutoConfigurationannotation along with @Configurationannotation. It has two optional elements,

您可以将@EnableAutoConfiguration注释与@Configuration注释一起使用。它有两个可选元素,

  • exclude : if you want to exclude the auto-configuration of a class.
  • excludeName : if you want to exclude the auto-configuration of a class using fully qualified name of class.
  • exclude :如果要排除类的自动配置。
  • excludeName :如果要使用类的完全限定名称排除类的自动配置。

Examples:

例子:

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
  public class MyConfiguration {
}


@EnableAutoConfiguration(excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@SpringBootApplicationis a newer version of @EnableAutoConfigurationwhich was introduced in Spring Boot 1.2.

@SpringBootApplication@EnableAutoConfigurationSpring Boot 1.2 中引入的较新版本。

@SpringBootApplicationis a combination of three annotations,

@SpringBootApplication是三个注解的组合,

  • @Configuration- for java based configuration classes.

  • @ComponentScan- to enable component scanning, all the packages and subpackages will be auto-scanned which are under the root package on which @SpringBootApplication is applied.

  • @EnableAutoConfiguration- to enable auto-configuration of the
    classes bases on the jars added in classpath.

  • @Configuration- 用于基于 Java 的配置类。

  • @ComponentScan- 要启用组件扫描,将自动扫描应用@SpringBootApplication 的根包下的所有包和子包。

  • @EnableAutoConfiguration-
    基于类路径中添加的 jar启用类的自动配置。

@ComponentScanenables component scanning so that web controller classes and other components that you create will be automatically discovered and registered as beans in spring's application context. You can specify the base packages that will be scanned for auto-discovering and registering of beans.

@ComponentScan启用组件扫描,以便您创建的 Web 控制器类和其他组件将被自动发现并在 spring 的应用程序上下文中注册为 bean。您可以指定将扫描以自动发现和注册 bean 的基本包。

One of the optional element is,

可选元素之一是,

  • basePackages - can be used to state specific packages to scan.
  • basePackages - 可用于说明要扫描的特定包。

Example,

例子,

@ComponentScan(basePackages = {"com.example.test"})
@Configuration
public class SpringConfiguration { }