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

    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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值