主流程入口

ApplicationContext context = new ClassPathXmlApplicationContext(“spring.xml”)

ClassPathXmlApplicationContext类:重载的构造方法依次调用,进入下面代码

AbstractApplicationContext的refresh方法:初始化spring容器的核心代码

public void refresh() throws BeansException, IllegalStateException {

    synchronized (this.startupShutdownMonitor) {

            //1、 Prepare this context for refreshing.
prepareRefresh(); //创建DefaultListableBeanFactory(真正生产和管理bean的容器)
//BeanDefinition处理
// 1 定位XML文件(Resource、ResourceLoader)
// 2 加载XML(Document、Dom4j解析)
// 3 从Document对象中解析BeanDefinition然后注册到BeanDefinitionRegistry(Map)
//通过NamespaceHandler解析自定义标签的功能(比如:context标签、aop标签、tx标签) //2、 Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//3、 Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory); try { //4、 Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory); //实例化并调用实现了BeanFactoryPostProcessor接口的Bean
//比如:PropertyPlaceHolderConfigurer(context:property-placeholer)
//就是此处被调用的,作用是替换掉BeanDefinition中的占位符(${})中的内容
//5、 Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory); //创建并注册BeanPostProcessor到BeanFactory中(Bean的后置处理器)
//比如:AutowiredAnnotationBeanPostProcessor(实现@Autowired注解功能)
//RequiredAnnotationBeanPostProcessor(实现@Required注解功能)
//这些注册的BeanPostProcessor
//6、 Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory); //7、 Initialize message source for this context.
initMessageSource(); //8、 Initialize event multicaster for this context.
initApplicationEventMulticaster(); //9、 Initialize other special beans in specific context subclasses.
onRefresh(); //10、 Check for listener beans and register them.
registerListeners(); //1 通过构造方法,创建Bean的实例。此处的Bean是非懒加载方式的单例Bean(未设置属性)
//2 填充属性(DI,依赖注入)---循环依赖(A中有B的依赖、B中有A的依赖)
//3 初始化实例(比如调用init-method方法)
// 调用BeanPostProcessor(后置处理器)对实例bean进行后置处理(AOP功能)
//11、 Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory); //12、 Last step: publish corresponding event.
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();
}
}
}

Spring容器初始话原理图的更多相关文章

  1. Spring--------web应用中保存spring容器

    ---恢复内容开始--- 问题:在一个web应用中我使用了spring框架,但有一部分模块或组件并没有托管给Spring,比如有的可能是一个webservice服务类,如果我想在这些非托管的类里使用托 ...

  2. ServletContext与Web应用以及Spring容器启动

    一.ServletContext对象获取Demo Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的ServletContext对象. 可以把ServletContext看作一个 ...

  3. Spring核心技术(七)——Spring容器的扩展

    本文将讨论如何关于在Spring生命周期中扩展Spring中的Bean功能. 容器的扩展 通常来说,开发者不需要通过继承ApplicationContext来实现自己的子类扩展功能.但是Spring ...

  4. Spring容器深入(li)

    spring中最常用的控制反转和面向切面编程. 一.IOC IoC(Inversion of Control,控制倒转).对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的 ...

  5. 通过单元测试理解spring容器以及dubbo+zookeeper单元测试异常处理

    一.先说一个结论:单元测试与主项目的spring容器是隔离的,也就是说,单元测试无法访问主项目spring容器,需要自己加载spring容器. 接下来是代码实例,WEB主项目出于运行状态,单元测试中可 ...

  6. spring源码学习之:spring容器的applicationContext启动过程

    Spring 容器像一台构造精妙的机器,我们通过配置文件向机器传达控制信息,机器就能够按照设定的模式进行工作.如果我们将Spring容器比喻为一辆汽车,可以将 BeanFactory看成汽车的发动机, ...

  7. Spring - Spring容器概念及其初始化过程

    引言 工作4年多,做了3年的java,每个项目都用Spring,但对Spring一直都是知其然而不知其所以然.鄙人深知Spring是一个高深的框架,正好近期脱离加班的苦逼状态,遂决定从Spring的官 ...

  8. Spring容器的创建刷新过程

    Spring容器的创建刷新过程 以AnnotionConfigApplicationContext为例,在new一个AnnotionConfigApplicationContext的时候,其构造函数内 ...

  9. Spring容器是如何实现 Bean 自动注入(xml)

    入口web.xml web.xml 配置文件 <!-- Spring Config --> <listener> <listener-class>org.sprin ...

随机推荐

  1. codeforces362B

    Petya and Staircases CodeForces - 362B 题意: 一个小男孩要上楼梯,他一次可以走1个台阶或2个台阶或3个台阶,但是有一些台阶是脏的,他不想走在脏台阶上.一共有n个 ...

  2. python数据类型知识整理

    python数据类型种类 int数字.bool布尔值.dict字典.tunple元组.set集合.list列表.字符串 int数字 #常用来进制转换 num = 11 #转化成2进制 bin_num ...

  3. 最简单的Spring Security配置示例

    代码结构: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  4. UNIX口令破解机

    在编写我们的UNIX口令破解机时,我们需要使用UNIX 计算口令hash 的crypt()算法.Python 标准库中已自带有crypt 库.要计算一个加密的UNIX 口令hash,只需调用函数cry ...

  5. #186 path(容斥原理+状压dp+NTT)

    首先只有一份图时显然可以状压dp,即f[S][i]表示S子集的哈密顿路以i为终点的方案数,枚举下个点转移. 考虑容斥,我们枚举至少有多少条原图中存在的边(即不合法边)被选进了哈密顿路,统计出这个情况下 ...

  6. Google浏览器——AxureRP_for_chorme_0_6_2添加

    准备 链接:https://share.weiyun.com/5PVwSMA Google浏览器版本 步骤 压缩解压 首先把需要安装的第三方插件,后缀.crx 改成 .rar,然后解压,得到一个文件夹 ...

  7. MT【301】值域宽度

    (2015浙江理科)已知函数$f(x)=x^2+ax+b,(a,b\in R)$.记$M(a,b)$是$|f(x)|$在区间$[-1,1]$上的最大值.(1)证明:当$|a|\ge2$时,$M(a,b ...

  8. TCP/IP 和 HTTP 的区别和联系是什么?

    作者:车小胖链接:https://www.zhihu.com/question/38648948/answer/240006409来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  9. 实验八 应用层协议Ⅱ-FTP协议分析

    实验八 应用层协议Ⅱ-FTP协议分析 一.实验目的 1.掌握FTP协议的实现原理. 2.了解控制通道和数据通道. 二.实验内容 用WareShark追踪ftp连接. 1.三次握手 2.ftp服务器回发 ...

  10. CPU监控 解题报告

    CPU监控 这种题就需要小黄鸭调试法,不行就重构,动态gdb可能会死人,一堆tag的... 维护历史最值的一个核心是历史最值tag,它的意义是从上一次这个点下放tag之后到当前时刻的这个点的tag达到 ...