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,就会报错,抛出异常。

猜你喜欢

转载自www.cnblogs.com/histlyb/p/9179492.html