Spring Boot AutoConfiguration自动配置解密


如果使用Spring Boot必须选择一个原因,这将是自动配置,这是Spring Boot后面的魔力,Spring Boot自动配置是一种基于类路径上存在的依赖关系自动配置应用程序的功能,无需开发人员自己付出任何努力。这是遵循约定优于配置  范式的Spring Boot方法  ,即将程序员在使用框架时必须做出的决策数量最小化,同时提供合理的默认值但不会失去灵活性。
所以,我们都知道@SpringBootApplication基本上由3个注释组成。

  • @EnableAutoConfiguration
  • @SpringBootConfiguration
  • @ComponentScan

我们现在将重点关注@EnableAutoConfiguration。简而言之,这个注释的作用是 :

“它将自动注入我们的应用程序所需的所有bean,具体取决于Spring Boot在类路径中找到的内容”

@EnableAutoConfiguration注释使用了Spring Core中的概念。许多Spring开发人员必须知道Spring 3中的@Enable *注释,如@EnableWebMvc和@EnableScheduling等。所以这些注释是为了特定的目的,比现在更具限制性。

@EnableAutoConfiguration的作用是在应用程序启动时:

  • SpringFactoriesLoader开始查找所有jar(从依赖项下载)查找路径为  META-INF / spring.factories的文件。
  • 当找到spring.factories这样的文件后,SpringFactoriesLoader将查找我们的配置文件命名的属性。它将读取spring.factories中的所有属性并开始匹配我们的配置中的属性,如果该属性与应用程序配置匹配,它将自动注入特定的bean。

可以看一下spring-boot-autoconfigure jar中的META-INF / spring.factories。 在此文件中,我们可以看到默认情况下可用的Spring Boot自动配置列表。

演示

  • 创建一个名为HelloService的简单Spring Boot项目,仅使用默认依赖项,即spring-boot-starter和spring-boot-starter-test。
  • 使用以下属性更新application.property以启用调试自动配置日志记录: logging.level.org.springframework.boot.autoconfigure.logging = DEBUG
  • 启动Spring Boot App。
  • 在控制台日志中查看CONDITIONS EVALUATION REPORT,它将在App中提供正负匹配列表,如下所示:

============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:
-----------------

   GenericCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration automatic cache type (CacheCondition)

   JmxAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.jmx.export.MBeanExporter' (OnClassCondition)
      - @ConditionalOnProperty (spring.jmx.enabled=true) matched (OnPropertyCondition)

   JmxAutoConfigurationmbeanExporter matched:
      - @ConditionalOnMissingBean (types: org.springframework.jmx.export.MBeanExporter; SearchStrategy: current) did not find any beans (OnBeanCondition)



Negative matches:
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   AopAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.lang.annotation.Aspect' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   BatchAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.springframework.batch.core.launch.JobLauncher' (OnClassCondition)

  • 从控制台复制条件评估报告并将其保存在您的某个地方。
  • 现在让我们在我们的应用程序中添加任何其他依赖项,例如JPA。
  • 再次重启应用程序。
  • 现在再次看一下条件评估报告。

我们可以看到之前处于 Negative Matches负面匹配中的几个bean现在处于Positive matches正匹配中。
其中一些是:
  • DataSourceAutoConfiguration
  • DataSourceTransactionManagerAutoConfiguration
  • HibernateJpaAutoConfiguration

还有一些与JPA有关。

SpringFactoriesLoader查看类路径中所有META_INF / spring.factories文件的属性,并尝试将其与应用程序中的可用配置相匹配。如果该属性在我们的应用程序中作为配置提供了,它会注入特定的bean或将其标记为negative matches负面匹配。