Spring容器初始话原理图
l 主流程入口:
ApplicationContext context = new ClassPathXmlApplicationContext(“spring.xml”)
l ClassPathXmlApplicationContext类:重载的构造方法依次调用,进入下面代码

l 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容器初始话原理图的更多相关文章
- Spring--------web应用中保存spring容器
---恢复内容开始--- 问题:在一个web应用中我使用了spring框架,但有一部分模块或组件并没有托管给Spring,比如有的可能是一个webservice服务类,如果我想在这些非托管的类里使用托 ...
- ServletContext与Web应用以及Spring容器启动
一.ServletContext对象获取Demo Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的ServletContext对象. 可以把ServletContext看作一个 ...
- Spring核心技术(七)——Spring容器的扩展
本文将讨论如何关于在Spring生命周期中扩展Spring中的Bean功能. 容器的扩展 通常来说,开发者不需要通过继承ApplicationContext来实现自己的子类扩展功能.但是Spring ...
- Spring容器深入(li)
spring中最常用的控制反转和面向切面编程. 一.IOC IoC(Inversion of Control,控制倒转).对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的 ...
- 通过单元测试理解spring容器以及dubbo+zookeeper单元测试异常处理
一.先说一个结论:单元测试与主项目的spring容器是隔离的,也就是说,单元测试无法访问主项目spring容器,需要自己加载spring容器. 接下来是代码实例,WEB主项目出于运行状态,单元测试中可 ...
- spring源码学习之:spring容器的applicationContext启动过程
Spring 容器像一台构造精妙的机器,我们通过配置文件向机器传达控制信息,机器就能够按照设定的模式进行工作.如果我们将Spring容器比喻为一辆汽车,可以将 BeanFactory看成汽车的发动机, ...
- Spring - Spring容器概念及其初始化过程
引言 工作4年多,做了3年的java,每个项目都用Spring,但对Spring一直都是知其然而不知其所以然.鄙人深知Spring是一个高深的框架,正好近期脱离加班的苦逼状态,遂决定从Spring的官 ...
- Spring容器的创建刷新过程
Spring容器的创建刷新过程 以AnnotionConfigApplicationContext为例,在new一个AnnotionConfigApplicationContext的时候,其构造函数内 ...
- Spring容器是如何实现 Bean 自动注入(xml)
入口web.xml web.xml 配置文件 <!-- Spring Config --> <listener> <listener-class>org.sprin ...
随机推荐
- 【NLP】How to Generate Embeddings?
How to represent words. 0 . Native represtation: one-hot vectors Demision: |all words| (too large an ...
- 扫描某目录下的所有文件的MD5码并导出文件【可执行jar】
pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:// ...
- BZOJ1444[Jsoi2009]有趣的游戏——AC自动机+概率DP+矩阵乘法
题目描述 输入 注意 是0<=P, n , l, m≤ 10. 输出 样例输入 input 1 3 2 2 1 2 1 2 AB BA AA input 2 3 4 2 1 2 1 2 AABA ...
- NTT算法小结
从理论上说,经过人们优化的FFT已经十分优秀,能够处理大部分的多项式乘法,但是有的时候仍然会出现下面的情况: 1)常数仍然比较大 2)在进行与整数有关的FFT时,发现得到的结果是一堆诡异的数,你需要不 ...
- 【XSY2524】唯一神 状压DP 矩阵快速幂 FFT
题目大意 给你一个网格,每个格子有概率是\(1\)或是\(0\).告诉你每个点是\(0\)的概率,求\(1\)的连通块个数\(\bmod d=0\)的概率. 最开始所有格子的概率相等.有\(q\)次修 ...
- web 压力测试工具
最近有收到任务,测试新服务器的性能. 花了很长时间做搜索,也整理了一些资料.以下是收集到一些简单易用的分析工具.推荐给大家使用. WebBenchhttp://www.ha97.com/4623.ht ...
- MVC接收列表参数
ASP.NET MVC 表单参数如果有列表时要怎么写呢. 虽然很久不用MVC了,但几乎每次遇到一次就要研究一下.然后又忘了. 其实也明白这是未完全弄清楚表单参数的传递形式,如果明白了,就知道MVC为 ...
- [TJOI2012]桥(最短路+线段树)
有n个岛屿, m座桥,每座桥连通两座岛屿,桥上会有一些敌人,玩家只有消灭了桥上的敌人才能通过,与此同时桥上的敌人会对玩家造成一定伤害.而且会有一个大Boss镇守一座桥,以玩家目前的能力,是不可能通过的 ...
- 绝对音乐No.1
最近儿子在练天空之城钢琴曲.为了方便他听久石让的原版,绝对做张cd.另外加入了自己比较喜欢的几首乐曲.在家音响上聆听时发现,不管是中国乐曲,还是西洋乐,都很美,耳朵都出油了.放到网盘供喜爱之人欣赏,喜 ...
- 终于解决了用JAVA写窗口程序在不同的windows界面下的显示保持一致。
好像是两三年前的时候发现这个问题. 由于在windows经典界面与windows xp界面下,窗口的标题栏的高度是不一样的. 所以我们在用Java写GUI程序的时候,会遇到一个问题. 当我把一个JFr ...