Spring Boot 精讲,看完你还敢说你不会 Spring Boot ?
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanDefinitionRegistryPostProcessors that implement
// PriorityOrdered, Ordered, and the rest.
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
这里获取到的 beanName 不是刚刚的增强器而是常量
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
并且下面这个循环会把
ConfigurationClassPostProcessor 加载出来。那么大家会问 ConfigurationClassPostProcessor 什么时候加载的呢?
createApplicationContext 这个方法还记得吧!就是在这里加载的当 Spring 选择了 Servlet 模式就会加载配置
CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME
就会作为 beanNamePut 进 beanFactory




First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.

Next, invoke the BeanFactoryPostProcessors that implement Ordered.

Finally, invoke all other BeanFactoryPostProcessors.

4.这里就是把启动类选出来并且开始 ComponentScan 的地方,自动装配也是这里完成的 @Import。

Parse each @Configuration class
这就是找 Component 的实现类

5.扫描 Component 和自动装配,这个也很重要,而且段代码有几个递归也是比较复杂,也是希望大家能够自己 debug 进去看看这到底是
如何递归的。
Process any @ComponentScan annotations
这个为什么是数组呢?当你使用 @ComponentScans(value ={@ComponentScan("com.example.test"), @ComponentScan("com.example.test1")})
这里的长度就会变成 3

开始以启动类扫描,这就是为什么启动类永远在所有包的最外层,如果要扫描其他模块或者启动类以外的包就要 @ScanComponent 这个注解。
The config class is annotated with @ComponentScan -> perform the scan immediately
这里面就开始扫描 doScan 方法,只要是有 @Component 注解的都会把每个类的 Definition 放到 scannedBeanDefinitions 里面。

这里有事解析这个 Bean,跟刚刚的 parser.parse 一样,只是一个是数组一个是单个 Bean。
并且他会递归调用
doProcessConfigurationClass()方法,并且重新执行一次获取看看这个类有没有 @ComponentScan,并且继续扫描直至结束。
parse(bdCand.getBeanClassName(), holder.getBeanName());
Process any @Import annotations
其实每一个 parse 方法都会走到这里,把每个类的注解都会去循环一次,直至没有注解位置,会把 @Import 注解的类全部加载出来,这就是自动装配的原理。
这就是为什么我其他 jar 包的类可以给 Spring 管理 @Import 就是一个重点,把这个类导入到 BeanFactory 里面。
processImports(configClass, sourceClass, getImports(sourceClass), filter, true);
创建 Bean 实例
========
创建 bean 实例,这里的方法过于复杂用文章非常难解释,笔者这里就把大致的思路说下。
1.getBean 的时候先去 createBean 如果有就返回没有的话 doCreateBean。
2.当 doCreateBean 的时候就会触发 bean 的生命周期的各个接口。
3.其实笔者发现一个东西,ApplicationContextAware 并不算是 bean 生命周期的一环把,而是输入上下文的一环。
4.因为 ApplicationContextAware 其实是由添加了
AnnotationConfigServletWebServerApplicationContext 类所导致的。
5.创建 bean 的我会有下面的 uml 图让大家更能理解 Bean 是如何创建的。



关于 ApplicationContext
====================
实现 ApplicationContextAware 接口会调用 setApplicationContext 方法,而
评论