主流程入口

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. P1914 一串字母

    P1914 题目背景 某蒟蒻迷上了“小书童”,有一天登陆时忘记密码了(他没绑定邮箱or手机),于是便把问题抛给了神犇你. 题目描述 蒟蒻虽然忘记密码,但他还记得密码是由一串字母组成.且密码是由一串字母 ...

  2. 怎么用Verilog语言描述同步FIFO和异步FIFO

    感谢 知乎龚大佬 打杂大佬 网上几个nice的博客(忘了是哪个了....) 前言 虽然FIFO都有IP可以使用,但理解原理还是自己写一个来得透彻. 什么是FIFO? Fist in first out ...

  3. 【XSY2774】学习 带花树

    题目描述 给你一个图,求最大匹配. 边的描述方式很特殊,就是一次告诉你\(c_i\)个点:\(d_1,d_2,\ldots,d_{c_i}\),表示这些点两两之间都有连边,也就是说,这是一个团.总共有 ...

  4. github Permission denied (publickey). fatal: Could not read from remote repository.

    github Permission denied (publickey).fatal: Could not read from remote repository. ----------------- ...

  5. 前后端分离之vue2.0+webpack2 实战项目 -- html模板拼接

    对于前后端分离,如何把一个页面的公共部分比如head, header, footer, content等组合成一个完整的html 是一个值得考虑的地方. 对于php,我们可以利用include加载其他 ...

  6. Square(斯特林反演)

    题意 给出一个 \(n × m\) 大小的矩形,每个位置可以填上 \([1, c]\) 中的任意一个数,要求填好后任意两行互不等价且任意两列互不等价,两行或两列等价当且仅当对应位置完全相同,求方案数 ...

  7. luogu3759 不勤劳的图书管理员 (树状数组套线段树)

    交换的话,只有它们中间的书会对答案产生影响 树状数组记位置,套线段树记书的编号 它对应的页数和书的个数 然后就是减掉中间那些原来是逆序对的,再把交换以后是逆序对的加上 别忘了考虑这两个自己交换以后是不 ...

  8. luogu3278/bzoj3323 多项式的运算 (splay)

    mulx的操作,其实就是给r+1的系数+=r的系数,然后删掉r,把l~r-1向右移一位,再插一个0到原来的位置 splay维护区间加和区间乘就好了 (一定要注意做事的顺序,一件事都做完了再去做别的,否 ...

  9. hdu 4300 Clairewd’s message(扩展kmp)

    Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...

  10. poj1664放苹果(递归)

    题目链接:http://poj.org/problem?id=1664 放苹果 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...