Spring Boot AutoConfiguration自动配置解密

栏目: Java · 发布时间: 7年前

内容简介:如果使用Spring Boot必须选择一个原因,这将是自动配置,这是Spring Boot后面的魔力,Spring Boot自动配置是一种基于类路径上存在的依赖关系自动配置应用程序的功能,无需开发人员自己付出任何努力。这是遵循所以,我们都知道@SpringBootApplication基本上由3个注释组成。我们现在将重点关注@EnableAutoConfiguration。简而言之,这个注释的作用是 :

如果使用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 <b>class</b> 'org.springframework.jmx.export.MBeanExporter' (OnClassCondition)
      - @ConditionalOnProperty (spring.jmx.enabled=<b>true</b>) matched (OnPropertyCondition)

   JmxAutoConfiguration#mbeanExporter 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 <b>class</b> 'javax.jms.ConnectionFactory' (OnClassCondition)

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

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

   BatchAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required <b>class</b> '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负面匹配。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

RESTful Web Services Cookbook

RESTful Web Services Cookbook

Subbu Allamaraju / Yahoo Press / 2010-3-11 / USD 39.99

While the REST design philosophy has captured the imagination of web and enterprise developers alike, using this approach to develop real web services is no picnic. This cookbook includes more than 10......一起来看看 《RESTful Web Services Cookbook》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具