Spring Bean的生命周期之我的理解(二)------AbstractApplicationContext
来源:互联网 发布:python insert into 编辑:程序博客网 时间:2024/06/05 06:15
前言
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
- Spring Bean的生命周期之我的理解(二)------AbstractApplicationContext
- Spring Bean的生命周期之我的理解(一)------BeanFactoryPostProcessor和BeanPostProcessor
- Spring Bean的生命周期之我的理解(三)------BeanFactory和FactoryBean
- Spring Bean的生命周期之我的理解(四)------AbstractAutowireCapableBeanFactory
- spring之bean的生命周期
- spring之Bean的生命周期
- spring之Bean的生命周期
- Spring Bean的生命周期小析(二)
- 浅谈spring——Bean的生命周期(二)
- Spring入门笔记(二) Bean的生命周期
- Spring Bean的生命周期
- Spring Bean的生命周期
- spring - bean的生命周期
- Spring Bean的生命周期
- spring bean 的生命周期
- Spring bean的生命周期
- spring bean的生命周期
- Spring Bean的生命周期
- android 编译 c 和 c++ 区别
- 我的emacs配置文件
- 理解http的幂等性
- debounce与throttle区别
- 第九周项目3-利用二叉树遍历思想解决问题
- Spring Bean的生命周期之我的理解(二)------AbstractApplicationContext
- ubuntu 16.04使用软件中心升级软件后桌面显示空白
- 贪婪的心最伤人
- TS流文件分析流程
- MySQL执行流程
- $(this).val()、this.id、$(this).attr(idCard)
- 嵌入式系统常用加密算法
- 人脸检测-- Face R-FCN + Face R-CNN
- (一)写在前面的话