Spring源码学习一——容器启动过程上

一、AnnotationConfigApplicationContext构造方法

启动一个注解配置的容器

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

进入构造方法
在这里插入图片描述

1.1 this()

先看下this(),执行之前会先执行父类的构造方法,完成beanFactory赋值
在这里插入图片描述
接着进入子类的构造方法
在这里插入图片描述
这块初始化了两个重要的对象,reader用于注册指定的类(无需Component注解)到容器,scanner用于注册指定路径下的类(带有Component/Repository/Service/Controller),这两个对象实例化时传入的是this,说明AnnotationConfigApplicationContext就是容器本身

1.2 register()

再往下,进入register(componentClasses);
在这里插入图片描述
使用reader将componentClass注册进容器,执行完之后
在这里插入图片描述
bean已经被添加进beanFactory的beanDefinitionMap。AnnotationConfigApplicationContext也是实现了BeanFactory接口,他的父类GenericApplicationContext持有了DefaultListableBeanFactory,看下GenericApplicationContext
在这里插入图片描述
明显这块使用了装饰器模式,增强了beanFactory。同时GenericApplicationContext又实现了BeanDefinitionRegistry,具有了读取注册bean的功能,又使用了适配器模式

1.3 refresh()

AbstractApplicationContext.refresh()方法将刷新启动容器
在这里插入图片描述
refresh方法启动容器调用了12个方法

  1. prepareRefresh();
  2. obtainFreshBeanFactory();
  3. prepareBeanFactory(beanFactory);
  4. postProcessBeanFactory(beanFactory);
  5. invokeBeanFactoryPostProcessors(beanFactory);
  6. registerBeanPostProcessors(beanFactory);
  7. initMessageSource();
  8. initApplicationEventMulticaster();
  9. onRefresh();
  10. registerListeners();
  11. finishBeanFactoryInitialization(beanFactory);
  12. finishRefresh();

下面我们看看每个方法的作用

二、容器启动生命周期

2.1 prepareRefresh()

prepareRefresh做的是容器刷新前的一些准备工作

  • 设置容器的启动时间
  • 设置活跃状态active为true
  • 设置关闭状态为false
  • 处理Environment对象
  • 准备监听器和事件的集合,默认为空

跟代码看下
在这里插入图片描述
看注释,意思是准备容器更新,设置启动时间和激活标志以及执行所有属性源的初始化。后半句其实是执行initPropertySources();
在这里插入图片描述
在上下文环境中初始化任何占位符属性源,这是一个模板方法,提供给子类扩展使用。如果有带占位符的属性源,可以重写这个方法。例如在applicationContext.xml文件中有sping.test.username=${username},可以在重写的 initPropertySources();方法中加上getEnvironment().getSystemProperties().put(“sping.test.username”,“java”);达到重新赋值的目的。

getEnvironment().validateRequiredProperties();创建环境对象,加载系统属性变量到环境对象中,并验证是否含有必要的属性

2.2 obtainFreshBeanFactory()

该方法用于获得一个新的 BeanFactory
在这里插入图片描述

2.2.1 refreshBeanFactory()

在这里插入图片描述
GenericApplicationContext的refreshBeanFactory()方法,不支持多次刷新,只调用一次refresh方法。给beanFactory设置一个唯一ID

2.2.2 getBeanFactory()

在这里插入图片描述
直接返回GenericApplicationContext持有的单例beanFactory。

2.3 prepareBeanFactory(beanFactory)

在这里插入图片描述
注释是说配置容器标志特征属性,如类加载器、后置增强等。主要完成一下设置:

  • beanFactory.addBeanPostProcessor:
    注册了一堆BeanPostProcessor到AbstractBeanFactory的beanPostProcessors中,以便bean在初始化前后调用BeanPostProcessor的方法。这些BeanPostProcessor需要传入ApplicationContext,在做增强时能用上。
  • beanFactory.ignoreDependencyInterface:
    设置各种Aware接口的实现类为忽略自动装配,放入ignoredDependencyInterfaces(Set类型)
  • beanFactory.registerResolvableDependency:
    设置自动装配的类(BeanFactory,ResourceLoader,ApplicationEventPublisher,ApplicationContext)。放入resolvableDependencies(Map类型),可以根据类型被注入
  • beanFactory.registerSingleton:
    注册各种可用组件(environment,systemProperties,systemEnvironment)

2.4 postProcessBeanFactory()

postProcessBeanFactory就是一个模板方法提供给子类扩展
在这里插入图片描述
自己扩展一个MyAnnotationConfigApplicationContext,就可以利用这个方法来修改beanFactory的属性,或者做一些其他操作

三、容器启动过程下

容器启动过程下

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值