SpringBoot
![](https://uploadfiles.nowcoder.com/files/20220512/665954992_1652364615942/v2-cdb3b53a6beba2213ad5ca4b61df6ce5_720w.png)
SpringBoot自动配置流程
![](https://uploadfiles.nowcoder.com/files/20220512/665954992_1652364616066/v2-3f2367fdc9cf4e108bdeef2373fb0ecb_720w.png)
BeanDefinition的注册顺序
-
先@ComponentScan的: @Component、@Serivce、@Controller
-
@Import的: @Component、@Service、@Controller
-
@Configuration: 和该配置类里面的@Bean->该配置类里面的@Import进来的实现了ImportBeanDefinitionRegistrar接口的
-
@Import进来的@Configuration: 和该配置类里面的@Bean->该配置类里面的@Import进来的实现了ImportBeanDefinitionRegistrar接口的
-
@Import进来的实现了DeferredImportSelector接口的@Configuration: 和该配置类里面的@Bean->该配置类里面的@Import进来的实现了ImportBeanDefinitionRegistrar接口的
自动装配原理
首先会通过import导入DeferredImportSelector.
为什么要导入DeferredImportSelector呢,这是因为为了顺序的一个考虑,它的加载顺序是最后的,把它放到最后呢才能进行定制我们自己的,而不是以它的Bean优先
然后去扫描所有jar包中的spring.factories文件,把其中所有全类限定名封装成一个list,然后进行排序返回给Spring,然后Spring会将它们注册是BeanDeifnition放到BeanDefinitionMap中去,然后Spring就能管理到这些Bean了