zoukankan      html  css  js  c++  java
  • SpringBoot高级-自动配置之@EnableAutoConfiguration注解

    前言:通过之前的自动配置原理相关介绍的文章,我们对自动配置的流程大概都很清楚了,这里我们对 @EnableAutoConfiguration 注解的实现流程进行跟踪,发掘源码中实现的原理。

    一、查看@EnableAutoConfiguration 注解的定义

    二、查看@Import导入的ImportSelector接口实现类 AutoConfigurationImportSelector

    三、查看实现类 AutoConfigurationImportSelector 的 selectImports() 方法

    四、查看 selectImports() 方法,发现其调用了 getAutoConfigurationEntry() 方法

    五、查看 getAutoConfigurationEntry() 方法, 发现其调用了 getCandidateConfigurations() 方法

    六、查看 getCandidateConfigurations() 方法

    断点调试,发现list对象configurations会有117个元素,这些元素就是Bean对象的全限定类名,通过对象的全限定类名将Bean初始化到IOC容器中

    在断言中,有一段提示信息"No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct."

    找到 spring.factories 文件的位置

    打开该文件,注意“”的使用说明。列表中,就有我们熟悉 RestTemplateAutoConfiguration、EmbeddedWebServerFactoryCustomizerAutoConfiguration 等自动配置类

    最后,打开一个EmbeddedWebServerFactoryCustomizerAutoConfiguration 看一看

    小结:虽然由于@EnableAutoConfiguration 注解的使用,初始化了spring.factories文件列表中的配置类,但是这些配置类中,使用了Condition条件,必须满足Condition条件,才能最终将Bean初始化。

  • 相关阅读:
    代理模式
    组合模式
    yum配置文件详解
    责任链模式
    git看不到别人创建的远程分支
    学习gulpfile.babel.js随笔
    遍历数组的方法
    解决Error: ENOENT: no such file or directory, scandir 安装node-sass报错
    window对象
    Moment.js的一些用法
  • 原文地址:https://www.cnblogs.com/elnimo/p/13792568.html
Copyright © 2011-2022 走看看