• spring IOC容器的扩展


    在此之前已经完成了IOCxml的解析和实例化工作,接下来需要分析Spring的高级版本对IOC容器的功能扩展:

    代码分析如下:

    synchronized (this.startupShutdownMonitor) {
    			// 准备刷新上下文环境
    			prepareRefresh();
    
    			// 初始化BeanFactory,并进行XML文件的读取 之前大部分IOC的核心逻辑都在这里
    			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
    			//对BeanFactory进行各种功能填充
    			prepareBeanFactory(beanFactory);
    
    			try {
    				// 留给子类进行服务覆盖和扩展的方法.
    				postProcessBeanFactory(beanFactory);
    
    				//激活各种BeanFactory处理器.
    				invokeBeanFactoryPostProcessors(beanFactory);
    
    				//激活Bean后处理器,在getBean时候调用,而BeanFactory后处理器是容器级别的,在此时就会被调用.
    				registerBeanPostProcessors(beanFactory);
    
    				//资源国际化处理.
    				initMessageSource();
    
    				// 初始化广播器,用于放所有的bean的监听器,并放入BeanFactory属性applicationEventMulticaster中.
    				initApplicationEventMulticaster();
    
    				// 留给子类去初始化其他bean,目前方法为空.
    				onRefresh();
    
    				// 查找bean的所有的监听器,并注册到广播器中去.
    				registerListeners();
    
    				// 初始化剩下的单实例.
    				finishBeanFactoryInitialization(beanFactory);
    
    				// 完成刷新过程,通知生命周期.
    				finishRefresh();
    			}
    

      

    接下来,即开始对上面的步奏进行一一的讲解:

    prepareRefresh();// 准备刷新上下文环境

    protected void prepareRefresh() {
            //留给子类覆盖的方法
            initPropertySources();
    
            // Validate that all properties marked as required are resolvable
            // 验证所需的属性已经存放到环境中
            getEnvironment().validateRequiredProperties();
    
            // Allow for the collection of early ApplicationEvents,
            // to be published once the multicaster is available...
            this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>();
        }

    下面举一个列子,来帮助理解,如何验证所需的属性已经存放到环境中

    public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext{
    
    	public MyClassPathXmlApplicationContext(String... configLocations) throws BeansException {
    		super(configLocations);
    	}
    
    	protected void initPropertySources(){
            // 在这里设置需要检测的环境变量 VAR
    		getEnvironment().setRequiredProperties("VAR");
    	}
    	
    	
    	protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory){
    		super.setAllowCircularReferences(false);
    		super.setAllowBeanDefinitionOverriding(false);
    		super.customizeBeanFactory(beanFactory);
    		
    	}
    }
    

      

    我们在使用MyClassPathXmlApplicationContext 对象加载bean的时候就会进行环境变量的验证

    在使用ApplicationContext ctx = new MyClassPathXmlApplicationContext("spring.xml");的时候,如果环境变量中没有怎加VAR,就会报错,抛出异常。

  • 相关阅读:
    「BZOJ1061」 [Noi2008]志愿者招募
    [POJ 2891] Strange Way to Express Integers (扩展中国剩余定理)
    扩展中国剩余定理学习笔记
    扩展欧几里得算法+推论
    SPOJ16607 IE1
    [Luogu P4124] [CQOI2016]手机号码 (数位DP)
    [UOJ 275/BZOJ4737] 【清华集训2016】组合数问题 (LUCAS定理的运用+数位DP)
    一些很妙的网站
    [Luogu P3157][CQOI2011]动态逆序对 (树套树)
    [Luogu P3203] [HNOI2010]弹飞绵羊 (LCT维护链的长度)
  • 原文地址:https://www.cnblogs.com/histlyb/p/9179492.html
Copyright © 2020-2023  润新知