Spring Bean的生命周期之我的理解(二)------AbstractApplicationContext

前言

Spring是如何初识化Bean的呢?这个问题是我们大家都关心的问题,最近又重新看了一下源码,AbstractApplicationContext借助于refresh方法加载和初识化Bean信息的。(注:Bean的实例化顺序都是先实例化被依赖的Bean,然后再实例化当前Bean)

refresh

    @Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            // 准备刷新用的上下文,设置一些活动标记,初始化上下文环境中的任何占位符属性资源,例如context:property-placeholder标签配置的信息
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            // 创建新的BeanFactory,并加载Bean定义
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            // 主要是BeanFactory初始化的工作,比如上下文的类加载器,设置表达式语言,注册默认的环境Beans等
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                // 目前是个空实现,后面再看
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                // 实例化所有实现BeanFactoryPostProcessor的类(实例化方式都是通过BeanFactory.getBean方法),包括Spring框架提供的内置的以及自定义实现BeanFactoryPostProcessor接口的类,并执行其postProcessBeanFactory方法。其中有一个比较重要的组件PropertySourcesPlaceholderConfigurer,用于替换配置文件中的占位符资源,比如环境变量资源${anything}
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                // 注册的BeanPostProcessor用于拦截Bean的创建,容器会扫描所有的BeanPostProcessor,包括自定义的BeanPostProcessor,然后进行注册,在其它Bean初始化前后被Spring容器所调用
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                // 初始化消息资源,比如你设置了<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">,则会加载相关的资源,否则容器提供默认的资源
                initMessageSource();

                // Initialize event multicaster for this context.
                // 初始化事件广播,默认使用SimpleApplicationEventMulticaster,当容器所有Bean初始化后,事件监听观察者会收到广播消息
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                // 初始化特殊的Bean,这里是Spring提供的扩展点
                onRefresh();

                // Check for listener beans and register them.
                // 注册事件监听器,所有实现了ApplicationListener接口或者Bean定义中未被实例化的单例Bean都会被注册,这里扫描到的所有事件都会被添加到广播中,会由广播将具体的触发事件告知事件监听者
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                // 初始化所有非延迟加载的单例Bean
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                // 完成初始化流程,包含初始化生命周期处理器并刷新,发布Bean初始化完成的广播事件,LiveBeansView没看明白,后面再看
                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();
            }
        }
    }

附录

本来想上传一份自己归纳的Bean是初始化时序图,但是图片太大了,上传不了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值