springfactory
springboot在进行SpringApplication对象初始化时会加载META-INF/springFactory文件,可在autoconfigure包中找到

SpringFactory文件集合了大部分的配置类

SpringApplication初始化实例时,调用initialize方法,里面调用getSpringFactoriesInstances方法

在SpringFactoriesLoader类中加载SpringFactory

将SpringFactory加载到spring容器中后,spring容器根据SpringFactory的内容进行自动配置,如配置Redis

在springboot的autoconfigure包中找到该配置类

加载该类,根据ConditionalOnClass注解判断其是否自动配置

如果spring容器中有RedisOperations这个类,那么就执行自动配置,因此
诸如solr等的配置,皆由上述类比

因此,springboot的自动配置其实就是封装一系列的配置包(jar包),根据条件注解配置类是否加载至spring容器中来实现
网友评论