ApplicationContext--容器的功能扩展

ApplicationContext和BeanFactory两者都是用于加载Bean的,但是相比之下,ApplicationContext提供了更多的扩展功能。

prepareRefresh–环境准备

1.initPropertySources留给子类重写,可以初始化PropertySource
2.validateRequiredProperties则是对属性进行验证

obtainFreshBeanFactory–加载BeanFactory

protected final void refreshBeanFactory() throws BeansException {
    if (hasBeanFactory()) {
        destroyBeans();
        closeBeanFactory();
    }
    try {
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        beanFactory.setSerializationId(getId());
        // 定制BeanFactory
        customizeBeanFactory(beanFactory);
        // 加载beandefinition
        loadBeanDefinitions(beanFactory);
        this.beanFactory = beanFactory;
    }
    catch (IOException ex) {
        throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
    }
}
定制BeanFactory

1.设置是否允许覆盖同名称不同定义的对象
2.提供了注解@Qualifier@Autowired的支持

加载BeanDefinition

1.将beandefinition加载入beanfactory中

prepareBeanFactory–功能扩展

增加SpEl语言的支持

1.为beanFactory设置BeanExpressionResolver,这个解析器何时用到呢?在bean进行属性填充的applyPropertyValues会用到, evaluateBeanDefinitinoString

增加属性注册编辑器
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

在注入时,有时候需要注入Date,File。而配置是String,这时候就需要用到属性注册编辑器了。此处Spring加了一些内置的属性注册编辑器。

添加ApplicationContextAwareProcesscor处理器

之前我们已经提到过,BeanPostProcessor会在bean填充完属性后被调用。 看一下这个BeanPostProcessor源码就知道,它是在调用aware接口的bean。

设置忽略依赖

忽略掉Aware的依赖注入

注册依赖
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);

通过上面的注册后,当你的类需要BeanFactory.class时,自然会能够给你注入

BeanFactory的后处理

激活注册的BeanFactoryPostProcessor

会调用BeanFactoryPostProcessor的postProcessBeanFactory方法

注册BeanPostProcessor
初始化消息资源
初始化ApplicationEventMulticaster
注册监听器

初始化非延迟加载单例

1.ConversionService的设置
2.冻结配置
3.初始化非延迟加载

即遍历调用getBean

finishRefresh

protected void finishRefresh() {
		// Clear context-level resource caches (such as ASM metadata from scanning).
		clearResourceCaches();

		// Initialize lifecycle processor for this context.
		initLifecycleProcessor();

		// Propagate refresh to lifecycle processor first.
		getLifecycleProcessor().onRefresh();

		// Publish the final event.
		publishEvent(new ContextRefreshedEvent(this));

		// Participate in LiveBeansView MBean, if active.
		LiveBeansView.registerApplicationContext(this);
	}
1.initLifecycleProcessor

初始化LifecycleProcessor

2.onRefresh

启动所有实现了Lifecycle接口的bean

3.publishEvent

发布ContextRefreshedEvent事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值