bean工厂创建及预准备工作
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
</dependencies>
//扫描包 使得 使用@Component注解生效
@ComponentScan("com.dmg")
@Configuration
public class TestConfig {
}
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(TestConfig.class);
}
}
spring容器的创建和刷新都是在这个refresh方法里面
![](https://img-blog.csdnimg.cn/img_convert/82b2be8d898036b918e25ed6deacbee0.png)
![](https://img-blog.csdnimg.cn/img_convert/a0c3798d29c17c4ea06bc4a88517eb5e.png)
先进入prepareRefresh方法,刷新前的预处理
![](https://img-blog.csdnimg.cn/img_convert/f68d74900d9689e66caee519f09089e7.png)
在initPropertySources();方法初始化一些属性
![](https://img-blog.csdnimg.cn/img_convert/a335496411d4ec588b6a84e2d1548782.png)
虽然上面的是空方法,但是可以在子类自定义设置一些内容
我们改造一下代码
public class My extends AnnotationConfigApplicationContext {
public My(Class<?>... componentClasses){
super(componentClasses);
}
@Override
protected void initPropertySources() {
System.out.println("我是子类。。。。。。。。。。。。。。。。。。。。。");
}
}
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext=new My(TestConfig.class);
}
}
这次在访问initPropertySources方法,就有数据了
![](https://img-blog.csdnimg.cn/img_convert/9528e2f0bb5b0be4b389c01d83ddb448.png)
接下来我们在回到
getEnvironment().validateRequiredProperties();这个方法
校验环境的属性的合法性
然后在这里创建早期的监听
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
创建早期的事件
this.earlyApplicationEvents = new LinkedHashSet<>();
![](https://img-blog.csdnimg.cn/img_convert/7309b411091d1dfe9ce360f95ca881d9.png)
然后在obtainFreshBeanFactory()获取bean工厂信息
![](https://img-blog.csdnimg.cn/img_convert/d0d2e5c27f85004982666eb744a2df8b.png)
![](https://img-blog.csdnimg.cn/img_convert/e9d2c69b4b40a6a6bbc4d0651941c97e.png)
刷新创建bean工厂,并设置序列化id
并返回bean工厂
![](https://img-blog.csdnimg.cn/img_convert/9ae34021e0c120724795b8db4d0e2d90.png)
将创建的bean工厂DefaultListableBeanFactory返回
![](https://img-blog.csdnimg.cn/img_convert/46f56b494289175d247251e2f3d4a11d.png)
接下来在回到prepareBeanFactory(beanFactory);方法
对bean工厂进行前置准备工作
![](https://img-blog.csdnimg.cn/img_convert/fd70eae284c17c01b214da41bfbebbfb.png)
设置bean工厂的类加载器,表达式解析器
![](https://img-blog.csdnimg.cn/img_convert/925da73dc175bd2000c54fd0cbf29f1e.png)
添加属性编辑器寄存器
添加部分bean后置处理器
![](https://img-blog.csdnimg.cn/img_convert/f0595709b545903e3facdfdc0d896e45.png)
设置忽略的自动装配接口
![](https://img-blog.csdnimg.cn/img_convert/451b1d11ecde8e4a3350bfeaddc18340.png)
注册可以解析的自动装配
![](https://img-blog.csdnimg.cn/img_convert/44a9edb03835f6e288a966f73addaae3.png)
给bean工厂注册一些能用的组件
![](https://img-blog.csdnimg.cn/img_convert/7c412375de38f0adae6a3424b7d04f68.png)
接下来在看下postProcessBeanFactory方法,在bean准备完成后的一些操作
他是个空方法,但是可以在子类中重写,自定义去处理
![](https://img-blog.csdnimg.cn/img_convert/7d2c6cfead266d0b47b176ce81eb6585.png)
public class My extends AnnotationConfigApplicationContext {
public My(Class<?>... componentClasses){
super(componentClasses);
}
@Override
protected void initPropertySources() {
}
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
System.out.println("我是子类"+beanFactory);
}
}
![](https://img-blog.csdnimg.cn/img_convert/b6a831d6334a89b293e50761681a7a45.png)
![](https://img-blog.csdnimg.cn/img_convert/ce7d1b0da4aeac58c76325ba90c9e9e8.png)
在这里就可以看到我们的bean工厂准备工作完成后,一些后置处理的工作了