• 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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    定制BeanFactory

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

    加载BeanDefinition

    1.将beandefinition加载入beanfactory中

    prepareBeanFactory–功能扩展

    增加SpEl语言的支持

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

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

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

    添加ApplicationContextAwareProcesscor处理器

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

    设置忽略依赖

    忽略掉Aware的依赖注入

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

    通过上面的注册后,当你的类需要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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1.initLifecycleProcessor

    初始化LifecycleProcessor

    2.onRefresh

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

    3.publishEvent

    发布ContextRefreshedEvent事件

  • 相关阅读:
    CodeWhisperer 使用经验分享
    【PAT(甲级)】1058 A+B in Hogwarts
    【Bluetooth蓝牙开发】八、BLE协议之传输层
    Contrastive Search Decoding——一种对比搜索解码文本生成算法
    Leecode1160: 拼写单词
    解决docker容器: bash: ping: command not found, 并制作镜像
    重装系统踩坑指南
    SAP MM学习笔记38 - 入库/请求自动决济(ERS - Evaluated Receipt Settlement)
    excel函数从0到掌握
    Python爬虫实战(基础篇)—13获取《人民网》【最新】【国内】【国际】写入Word(附完整代码)
  • 原文地址:https://blog.csdn.net/Xjzzon/article/details/126575256