spring源码-4-applicationContext(环境上下文)

来源:互联网 发布:花千骨衣服淘宝 编辑:程序博客网 时间:2024/06/05 07:43
    ApplicationContext  applicationContext= new ClassPathXmlApplicationContext("application.xml");

1. ClassPathXmlApplication也是beanFactory的一个实现

//构造方法public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)            throws BeansException {        super(parent);        setConfigLocations(configLocations);  //设置资源路径        if (refresh) {            refresh(); //刷新上下文 填充功能        }    }

2.. AbstractRefreshableConfigApplicationContex.设置并解析资源t

//为applicationContext 指定资源路径public void setConfigLocations(String... locations) {    if (locations != null) {        Assert.noNullElements(locations, "Config locations must not be null");        this.configLocations = new String[locations.length];        for (int i = 0; i < locations.length; i++) {            this.configLocations[i] = resolvePath(locations[i]).trim(); //解析指定的资源 PropertyResolver        }    }    else {        this.configLocations = null;

3. AbstractApplicationContext 填充上下文环境功能

public void refresh() throws BeansException, IllegalStateException {        synchronized (this.startupShutdownMonitor) {            // Prepare this context for refreshing.            prepareRefresh();  //刷新新的环境上下文            // Tell the subclass to refresh the internal bean factory.            //初始化beanFactory            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();            // Prepare the bean factory for use in this context.            //填充beanFacoty   classLoader propertyEditor等            prepareBeanFactory(beanFactory);            try {                // Allows post-processing of the bean factory in context subclasses.                //可以实现此类 重写该方法提供特殊的额外处理                postProcessBeanFactory(beanFactory);                // Invoke factory processors registered as beans in the context.                // 激活beanFacoty                invokeBeanFactoryPostProcessors(beanFactory);                // Register bean processors that intercept bean creation.                //注册beanFacoty                registerBeanPostProcessors(beanFactory);                // Initialize message source for this context.                //为上下文初始化message资源                initMessageSource();                // Initialize event multicaster for this context.                initApplicationEventMulticaster();                // Initialize other special beans in specific context subclasses.                //可以实现此类 进行特殊的初始化                onRefresh();                // Check for listener beans and register them.                //注册所有的监听器                registerListeners();                // Instantiate all remaining (non-lazy-init) singletons.                //初始化所有非 lazy-init 的单例                finishBeanFactoryInitialization(beanFactory);                // Last step: publish corresponding event.                //1.完成刷新过程 clearResourceCaches();                //2.声明声明周期处理器   getLifecycleProcessor().onRefresh();                //3.发布事件  publishEvent(new ContextRefreshedEvent(this));                finishRefresh();            }            catch (BeansException ex) {                if (logger.isWarnEnabled()) {                    logger.warn("Exception encountered during context initialization - " +                            "cancelling refresh attempt: " + ex);                }                // Destroy already created singletons to avoid dangling resources.                //出现异常则 清除已创建的单例                destroyBeans();                // Reset 'active' flag.                //更新刷新状态                cancelRefresh(ex);                // Propagate exception to caller.                throw ex;            }            finally {                // Reset common introspection caches in Spring's core, since we                // might not ever need metadata for singleton beans anymore...                resetCommonCaches(); //清除缓存资源            }        }    }
0 0
原创粉丝点击