Springboot的@EnableAutoConfiguration注解都做了什么
@EnableAutoConfiguration
Spring框架提供的各种名字为@Enable开头的Annotation定义,比如@EnableScheduling、@EnableCaching、@EnableMBeanExport等,@EnableAutoConfiguration的理念和做事方式其实一脉相承,简单概括一下就是,借助@Import的支持,收集和注册特定场景相关的bean定义。
@EnableScheduling是通过@Import将Spring调度框架相关的bean定义都加载到IoC容器。
@EnableMBeanExport是通过@Import将JMX相关的bean定义加载到IoC容器。
而@EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器,仅此而已!
@EnableAutoConfiguration作为一个复合Annotation,其自身定义关键信息如下:
其中,最关键的要属@Import(EnableAutoConfigurationImportSelector.class),借助EnableAutoConfigurationImportSelector,@EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。就像一只“八爪鱼”一样
借助于Spring框架原有的一个工具类:SpringFactoriesLoader的支持,@EnableAutoConfiguration可以智能的自动配置功效才得以大功告成!
来看看META-INF/spring.factories里面的内容
由上图可知,spring.factories中有很多配置好的路径,如下图
其中,
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration 包含了很多XXXAutoCofiguration。全部都是自动配置类
以jdbc为例,如下图包含了DataSourceAutoConfiguration.class和对应的DataSourceProperties
(一般来说,XXXAutoConfiguration类都会有一个对应的XXXProperties),XXXProperties中放了默认配置的默认值
参考
---------------------
作者:happyAliceYu
来源:****
原文:https://blog.****.net/happyaliceyu/article/details/81219610
版权声明:本文为博主原创文章,转载请附上博文链接!