一、ApplicationContext的中文意思是“应用上下文”,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持、资源访问(如URL和文件)、事件传播等方面进行了良好的支持,被推荐为Java EE应用之首选,可应用在Java APP与Java Web中。

  二、ApplicationContext相对于XmlBeanFactory做了很多是拓展的功能,而这些拓展的功能,不管是在容器初始化,还是中间需要处理的过程,都做了很大的改善以及深入。

  三、先看一下ApplicationContext的继承关系图,了解一下和XmlBeanFactory的区别

  注意:这是一张ClassPathXmlApplicationContext的继承关系图,目前是为了更好的呈现,当前bean在加载的时候做的那些事情。

  四、可能有人会问,为什么没有DefaultListableBeanFactory的相关属性了呢,这个在源码的讲解部分会细说。

  五、源码解析

  1)增强性的bean容器初始化实现方式

     public static void main(String[] args) {
//增强性的bean容器初始化化
ApplicationContext context = new ClassPathXmlApplicationContext("spring-bean.xml");
}

  2)实现过程

    public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[]{configLocation}, true, (ApplicationContext)null);
} public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
super(parent);
//设置需要加载的xml文件
this.setConfigLocations(configLocations);
if (refresh) {
//容器的整个初始化过程都在这里实现
this.refresh();
} }

  3)refresh

    public void refresh() throws BeansException, IllegalStateException {
Object var1 = this.startupShutdownMonitor;
synchronized(this.startupShutdownMonitor) {
//前期准备工作
this.prepareRefresh();
//xml解析过程
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
//beanFactory的准备过程
this.prepareBeanFactory(beanFactory); try {
//BeanFactoryPostProcessor接口拓展
this.postProcessBeanFactory(beanFactory);
this.invokeBeanFactoryPostProcessors(beanFactory);
//注册BeanPostProcessor的实现接口,实际调用在getBean的时候
this.registerBeanPostProcessors(beanFactory);
//国际化
this.initMessageSource();
//初始化应用广播器
this.initApplicationEventMulticaster();
//子类应用
this.onRefresh();
//监听部分
this.registerListeners();
//完成初始化
this.finishBeanFactoryInitialization(beanFactory);
//完成刷新
this.finishRefresh();
} catch (BeansException var4) {
this.destroyBeans();
this.cancelRefresh(var4);
throw var4;
}
}
}

  六、因为ApplicationContext在做容器初始化的时候做了很多工作,所以我这里会形成一个目录,来讲解剩下的部分。

  1)spring源码-增强容器xml解析-3.1

  2)spring源码-BeanFactoryPostProcessor-3.2

  3)spring源码-BeanPostProcessor-3.3

  4)spring源码-Aware-3.4

  5)spring源码-国际化-3.5

  6)spring源码-事件&监听3.6

spring源码-bean之增强初始化-3的更多相关文章

  1. Spring源码-IOC部分-容器初始化过程【2】

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...

  2. Spring源码系列 — 构造和初始化上下文

    探索spring源码实现,精华的设计模式,各种jdk提供的陌生api,还有那么点黑科技都是一直以来想做的一件事!但是读源码是一件非常痛苦的事情,需要有很大的耐心和扎实的基础. 在曾经读两次失败的基础上 ...

  3. Spring 源码学习 04:初始化容器与 DefaultListableBeanFactory

    前言 在前一篇文章:创建 IoC 容器的几种方式中,介绍了四种方式,这里以 AnnotationConfigApplicationContext 为例,跟进代码,看看 IoC 的启动流程. 入口 从 ...

  4. spring源码-bean之初始化-1

    一.spring的IOC控制反转:控制反转——Spring通过一种称作控制反转(IOC)的技术促进了松耦合.当应用了IOC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查 ...

  5. spring源码-bean之加载-2

    一.前面说了bean的容器初始化,后面当然是说bean的加载.这里还是不讲解ApplicationContext的bean的加载过程,还是通过最基础的XmlBeanFactory来进行讲解,主要是熟悉 ...

  6. Spring源码--Bean的管理总结(一)

    前奏 最近看了一系列解析spring管理Bean的源码的文章,在这里总结下,方便日后复盘.文章地址https://www.cnblogs.com/CodeBear/p/10336704.html sp ...

  7. Spring源码-Bean生命周期总览

  8. spring源码-开篇

    一.写博客也有一段时间了,感觉东西越来越多了,但是自己掌握的东西越来越少了,很多时候自己也在想.学那么多东西,到头来知道的东西越来越少了.是不是很奇怪,其实一点都不奇怪. 我最近发现了一个很大的问题, ...

  9. Spring源码-AOP部分-Spring是如何对bean实现AOP代理的

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 历史文章 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] S ...

随机推荐

  1. BZOJ1009:[HNOI2008]GT考试(AC自动机,矩乘DP)

    Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字. 他的不吉利数学A1A2...Am(0< ...

  2. nrf52840蓝牙BLE5.0空中数据解析

    一.基础知识: 我没找到蓝牙5.0的ATT数据格式图片,在蓝牙4.0的基础上做修改吧,如下图所示:   二.测试与分析: 参数设置: data length = 251字节,MTU = 247字节, ...

  3. linux常用搜索文件命令

    使用linux系统难免会忘记文件所在的位置,可以使用以下命令对系统中的文件进行搜索.搜索文件的命令为”find“:”locate“:”whereis“:”which“:”type“ 方法/步骤     ...

  4. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  5. shiro集成spring&工作流程&DelegatingFilterProxy

    1.集成Spring 参考文献: 新建web工程: ehcache-core来自Hibernate wen.xml <?xml version="1.0" encoding= ...

  6. ASP.NET Web API编程——文件上传

    首先分别介绍正确的做法和错误的做法,然后分析他们的不同和错误之处,以便读者在实现此功能时可避开误区 1正确的做法 public class AvaterController : BaseApiCont ...

  7. mongodb、parse-server、parse-dashboard 的启动命令

    1.mongodb启动: 1$ C:\MongoDB\Server\bin>mongod --logpath d:\mongodb\logs\log.log $ C:\MongoDB\Serve ...

  8. phpstorm下TODO注释

    TODO注释 什么是TODO注释? 标记的注释,表示你代办的任务. 作用 标记你需要编写的任务位置 使用方法 TODO: + 说明: 如果代码中有该标识,说明在标识处有功能代码待编写,待实现的功能在说 ...

  9. nodejs实战的github地址,喜欢的你还等啥

    第一章.第二章:使用Express + MongoDB搭建多人博客:https://github.com/nswbmw/N-blog 第三章:使用Redis搭建漂流瓶服务器:https://githu ...

  10. HDU 1250 Hat's Fibonacci(大数相加)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Ot ...