Spring源码系列 — 构造和初始化上下文
探索spring源码实现,精华的设计模式,各种jdk提供的陌生api,还有那么点黑科技都是一直以来想做的一件事!但是读源码是一件非常痛苦的事情,需要有很大的耐心和扎实的基础。
在曾经读两次失败的基础上,这次希望能一站到底!这个系列基于spring v4.3.20版本探索。
Spring上下文启动加载过程的分段
spring上下文的实现非常多,其中基于Xml启动的有ClassPathXmlApplicationContext、FileSystemXmlApplicationContext等等。这些上下文都非常类似,基于解析Xml,获取配置的bean,最终完成上下文的启动加载过程。
这里以ClassPathXmlApplicationContext为主分析Spring ApplicatonContext整个启动过程。这里将spring上下文的启动分为两个大步骤:
- 上下文对象本身的构造和初始化:创建ClassPathXmlApplicationContext对象,构造器中执行一些初始化操作,如:设置上下文的环境Enviroment、设置上下文的资源解析器等
- 上下文获取bean配置,解析实例化bean:构造BeanFactory,解析Xml,构造bean定义,依赖注入,实例化bean。这个过程非常复杂;
这节主要分析第一个阶段:上下文对象本身的构造和初始化。
上下文对象本身的构造和初始化
编写debug代码,构造ClassPathXmlApplicationContext对象:
ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
HelloWorldBean h = context.getBean("helloWorld", HelloWorldBean.class);
h.printHelloWorld();
编写配置文件beans.xml,配置helloWorld的bean:
<bean id="helloWorld" class="com.learn.ioc.beans.HelloWorldBean"></bean>
下面主要分析ClassPathXmlApplicationContext new的过程。ClassPathXmlApplicationContext构造函数如下:
// 使用beans.xml作为上下文的配置文件构造ApplicationContext对象
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {configLocation}, true, null);
}
// 以配置文件、是否刷新上下文、父上下文作为参数构造ApplicationContext对象
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
super(parent)和setConfigLocations(configLocations)方法对应两阶段中的第一阶段:上下文对象本身的构造和初始化。refresh()方法完成第二阶段:上下文获取bean配置,解析实例化bean。
super(parent)以该上下文的父上下文作为参数调用父类的构造器,这里由于没有父上下文,所以为null:
public AbstractXmlApplicationContext(ApplicationContext parent) {
super(parent);
}
AbstractXmlApplicationContext是以Xml作为配置的Spring上下文,AbstractXmlApplicationContext又继续调用其父类的构造器:
public AbstractRefreshableConfigApplicationContext(ApplicationContext parent) {
super(parent);
}
AbstractRefreshableConfigApplicationContext也是以Xml文件作为基础配置的上下文,但是它具有可以刷新配置文件的能力,AbstractRefreshableConfigApplicationContext中提供了setConfigLocations方法可以用于设置配置文件。是Xml配置文件上下文的基石。
该上下文中又调用父类构造函数:
public AbstractRefreshableApplicationContext(ApplicationContext parent) {
super(parent);
}
Spring上下文可以称作为上下文家族,继承有多代。其中AbstractRefreshableApplicationContext是上下文中家族中的元老。它提供了两个非常重要的接口refreshBeanFactory()和loadBeanDefinitions(DefaultListableBeanFactory beanFactory)用于配置BeanFactory和定义接下载入Bean的接口。虽然构造函数中依然调用父类的构造函数,但是它的确非常重要:
public AbstractApplicationContext(ApplicationContext parent) {
this();
// 设置该上下文的父上下文
setParent(parent);
}
public AbstractApplicationContext() {
// 初始化资源解析器,用于解析获取Xml配置
this.resourcePatternResolver = getResourcePatternResolver();
}
上下文家族的鼻祖AbstractApplicationContext中定义了整个Bean的声明周期和处理过程。在构造器中初始化resourcePatternResolver资源解析器,赋予ApplicationContext具有解析资源的能力。且设置父上下文。
getResourcePatternResolver主要用来创建资源解析器:
protected ResourcePatternResolver getResourcePatternResolver() {
return new PathMatchingResourcePatternResolver(this);
}
PathMatchingResourcePatternResolver主要以路径匹配的模式进行解析获取资源,其主要能力和实现后续会详细介绍。
setParent(parent)主要用于设置该上下文的父上下文:
public void setParent(ApplicationContext parent) {
// 设置父上下文
this.parent = parent;
// 如果父上下文不空
if (parent != null) {
// 获取父上下文的环境变量Environment
Environment parentEnvironment = parent.getEnvironment();
if (parentEnvironment instanceof ConfigurableEnvironment) {
// 将父上下文的环境变量Environment合并至该级上下文环境变量中
getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
}
}
}
经过一系列的父类的构造器的调用,Spring上下文完成了多级上下文的载入过程。可以从下图看出其继承顺序关系:
Tips
上下文的实现中设计模式非常强,秉着设计的六大原则进行实现:
单一职责原则:每种上下文都有自己的职责能力,使得上下文的扩展能力极强;
开闭原则:AbstractRefreshableApplicationContext提供了对loadBeanDefinitions的定义由其子类按照不同加载Bean的逻辑各自实现;
依赖倒置原则:通过多级的抽象,提供了不同的接口,达到针对接口编程;
ClassPathXmlApplicationContext中调用层级父类上下文对象的构造后,再执行AbstractRefreshableConfigApplicationContext中实现的setConfigLocations设置上下文的Xml配置:
// 设置配置文件路径
public void setConfigLocations(String... locations) {
// 如果不为空,则构早配置文件路径,支持多个配置文件
if (locations != null) {
Assert.noNullElements(locations, "Config locations must not be null");
this.configLocations = new String[locations.length];
// 循环解析多个配置文件路径
for (int i = 0; i < locations.length; i++) {
this.configLocations[i] = resolvePath(locations[i]).trim();
}
}
else {
this.configLocations = null;
}
}
关于resolvePath的具体过程涉及到Spring IOC容器的环境Enviroment组件,具体的解析路径的过程将在下章的探索Enviroment中详解。
总结
Spring上下文类型非常繁多,其中有直接面向各种场景直接使用的FileSystemApplicationContext、ClasspathXmlApplicationContext等等,还有很多实现基础能力的上下文:
AbstractApplicationContext是上下文实现中的基石,其中定义了上下文Bean对象依赖注入的模板;
AbstractRefreshableApplicationContext也是非常重要的上下文,其中组合了BeanFactory和定义加载Bean的接口;
Spring源码系列 — 构造和初始化上下文的更多相关文章
- Spring源码系列 — 注解原理
前言 前文中主要介绍了Spring中处理BeanDefinition的扩展点,其中着重介绍BeanDefinitionParser方式的扩展.本篇文章承接该内容,详解Spring中如何利用BeanDe ...
- Spring源码系列 — BeanDefinition扩展点
前言 前文介绍了Spring Bean的生命周期,也算是XML IOC系列的完结.但是Spring的博大精深,还有很多盲点需要摸索.整合前面的系列文章,从Resource到BeanDefinition ...
- Spring源码系列 — Bean生命周期
前言 上篇文章中介绍了Spring容器的扩展点,这个是在Bean的创建过程之前执行的逻辑.承接扩展点之后,就是Spring容器的另一个核心:Bean的生命周期过程.这个生命周期过程大致经历了一下的几个 ...
- Spring源码系列 — BeanDefinition
一.前言 回顾 在Spring源码系列第二篇中介绍了Environment组件,后续又介绍Spring中Resource的抽象,但是对于上下文的启动过程详解并未继续.经过一个星期的准备,梳理了Spri ...
- Spring源码系列(二)--bean组件的源码分析
简介 spring-bean 组件是 Spring IoC 的核心,我们可以使用它的 beanFactory 来获取所需的对象,对象的实例化.属性装配和初始化等都可以交给 spring 来管理. 本文 ...
- 事件机制-Spring 源码系列(4)
事件机制-Spring 源码系列(4) 目录: Ioc容器beanDefinition-Spring 源码(1) Ioc容器依赖注入-Spring 源码(2) Ioc容器BeanPostProcess ...
- Ioc容器BeanPostProcessor-Spring 源码系列(3)
Ioc容器BeanPostProcessor-Spring 源码系列(3) 目录: Ioc容器beanDefinition-Spring 源码(1) Ioc容器依赖注入-Spring 源码(2) Io ...
- AOP执行增强-Spring 源码系列(5)
AOP增强实现-Spring 源码系列(5) 目录: Ioc容器beanDefinition-Spring 源码(1) Ioc容器依赖注入-Spring 源码(2) Ioc容器BeanPostProc ...
- Ioc容器beanDefinition-Spring 源码系列(1)
Ioc容器beanDefinition-Spring 源码系列(1) 目录: Ioc容器beanDefinition-Spring 源码(1) Ioc容器依赖注入-Spring 源码(2) Ioc容器 ...
随机推荐
- 【centOS】centOS7 下载
地址:http://mirrors.aliyun.com/centos/ 进入国内的阿里云的,这里CentOS 7提供了三种ISO镜像文件的下载:DVD ISO.Everything ISO.Mini ...
- Newtonsoft.Json 序列化踩坑之 IEnumerable
Newtonsoft.Json 序列化踩坑之 IEnumerable Intro Newtonsoft.Json 是 .NET 下最受欢迎 JSON 操作库,使用起来也是非常方便,有时候也可能会不小心 ...
- 安装Keepalived namespaces.c:187: error: ‘SYS_setns’ undeclared (first use in this function)
错误信息 namespaces.c: In function ‘setns’: namespaces.c:: error: ‘SYS_setns’ undeclared (first use in t ...
- .net core 后台如何生成html字符串到前台_后台html字符串在前台显示编码状态
1-后台生成html字符串的方法一般是: this.ViewData.Add("CURRENCY_SYMBOLS", new HtmlString(JsonHelper.Conve ...
- 诚聘.NET架构师、高级开发工程师(2019年8月29日发布)
招聘单位是ABP架构设计交流群(134710707)群主阳铭所在的公司 公司简介 七二四科技有限公司成立于2015年,成立之初便由金茂资本按估值2亿投资2200万,进行“健康724”平台搭建,2017 ...
- Python读写Excel文件的实例
最近由于经常要用到Excel,需要根据Excel表格中的内容对一些apk进行处理,手动处理很麻烦,于是决定写脚本来处理.首先贴出网上找来的读写Excel的脚本. 1.读取Excel(需要安装xlrd) ...
- Jquery绑定事件及动画效果
Jquery绑定事件及动画效果 本文转载于:https://blog.csdn.net/Day_and_Night_2017/article/details/85799522 绑定事件 bind(ty ...
- 【mysql】pymysql.err.InterfaceError Interface Error: (0, '')
八成是丢失连接了 while 1: try: self.conn.ping(reconnect=True) self.cur.execute(sql,tuple(item.values())) sel ...
- JSAPI签权
JSAPI鉴权----钉钉H5开发 虽钉钉开发文档上写着不需要JSAPI签权,但这仅仅是针对Android手机 所以为了保险起见,在使用JSAPI前,都需要签权. 否则:dd.ready({}) 将不 ...
- Git入门基础教程和SourceTree应用
目录 一.Git的安装 1.1 图形化界面 1.2 命令行界面 二.本地仓库的创建与提交 2.1 图形化界面 2.1.1 首先在电脑上有一个空白目录 2.1.2 打开SourceTree 2.1.3 ...