Spring源码阅读-spring启动
最近抽空看了下spring源码,我们一起从web容器启动开始,一步一步分析spring是如何被拉起,如何加载加载其配置文件,如何使用加载到容器上下文中的对象
web.xml
web.xml中的spring容器配置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListner如何拉起?
ContextLoaderListener是spring核心功能启动器,该监听器会监听Servlet容器启动事件,一旦监听到该事件就拉起spring整个容器的启动。

- ContextLoaderListener实现了javax.servlet.ServletContextListener接口,该接口的contextInitialized方法用来对扩展的容器进行初始化,ContextLoaderListener在该方法中对spring上下文进行初始化。
- ContextLoaderListener继承ContextLoader,ContextLoader.initWebApplicationContext方法创建容器Context,并且调度配置文件加载。
ContextLoader.initWebApplicationContext方法中Context创建代码如下:
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
初始化容器时需要判断容器是否已经初始化过,如果没有则创建一个新的spring容器
容器创建流程如下:

ContextLoader会首先从ServletContext中获取配置的容器类(参数名:contextClass)
如果获取不到则使用ContextLoader同一classPath下的ContextLoader.properties中的配置。
将org.springframework.web.context.WebApplicationContext对应的值作为容器类并实例化容器作为spring的根容器。ContextLoader.properties配置如下:

Context创建
创建Context实例,刷新Context。
刷新Context:创建Bean工厂,调用ResourceReader加载Spring的bean配置,将每个bean配置构建成一个BeanDefinition对象存储到Bean工厂,bean配置加载完成后需要给Bean工厂设置一系列postProccessor,执行这些Proccessor,Context在postProccessor处理完成后对所有“非懒加载”的单例bean对象进行实例化。实例化后执行BeanPostProcessor的BeanPostProcessorsBeforeInitialization方法,而后对指定了init方法的实例调用init方法,或者对实现了InitializingBean接口的bean调用afterPropertiesSet方法,初始化方法后执行BeanPostProcessor的applyBeanPostProcessorsAfterInitialization方法。实例化结束后向所有监听器推送ContextRefreshedEvent事件。其中供bean实例使用的BeanPostProcessor是ApplicationListenerDetector,通过该Proccessor,bean可以自定义init方法调用前后执行的动作。init执行前可以通过实现InitializingBean接口,init执行后可以通过实现ApplicationListener接口。
Bean工厂继承关系:

对于bean的具体实例化、初始化、
默认的XmlWebApplicationContext
XmlWebApplicationContext实例化后被存放到ServletContext中,缓存使用的key为:org.springframework.web.context.WebApplicationContext.ROOT
在web服务中只要能获取到ServletContext就可以根据WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE获取spring根Context
XmlWebApplicationContext实例化成功后,ContextLoader会调用configureAndRefreshWebApplicationContext对容器上下文进行配置和初始化
其中就包括Spring基本属性设置、配置文件加载,解析,对象实例化等动作。Spring容器上下文继承关系如下:

XmlWebApplicationContext的配置文件加载,解析,对象实例化等动作都统一在AbstractApplicationContext.refresh方法中完成
refresh结束后,容器会发布一个ContextRefreshedEvent事件,供依赖容器启动结束的组件或者第三方定制扩展使用(例如Dubbo,通过监听该事件触发其配置加载后的实例包装动作)。
具体的刷新核心代码我们看一下:
// Prepare this context for refreshing.
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory); try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
initMessageSource(); // Initialize event multicaster for this context.
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
onRefresh(); // Check for listener beans and register them.
registerListeners(); // Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
finishRefresh();
}
BeanFactory
容器刷新涉及最终要的部件是ApplicationContext辅助类以及beanFactory,ApplicationContext负责spring配置文件的加载和解析,将bean配置解析转换成一个个BeanDefinition并传递给bean工厂,bean工厂调度ApplicationContext辅助类的解析工作,并在解析完成后负责bean对象实例化。
obtainFreshBeanFactory方法完成工厂创建和刷新,工厂刷新工作内容主要是调度ApplicationContex将bean配置解析转换成一个个BeanDefinition,具体的配置解析和加载工作实现在不同的ApplicationContext实现类,例如xml解析在XmlWebApplicationContext中定义。

通过调用父类org.springframework.web.context.support.XmlWebApplicationContext的loadBeanDefinitions实际完成加载工作,其实所有ApplicationContext的对应的配置加载实现都在org.springframework.web.context.support包下面:

org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver负责加载spring配置文件解析器信息配置
扩展NameSpaceHandler
其中配置了配置文件命名空间和NamespaceHandler类的映射关系。
NamespaceHandler中定义了不同类型xml节点的解析器。容器启动过程中XmlWebApplicationContext会通过XmlBeanDefinitionReader读取Spring的配置文件,在解析每一个配置节点时,会从NamespaceHandler中获取对应的解析器进行解析并转换成Spring容器中的实例或者spring配置对象。
Spring源码阅读-spring启动的更多相关文章
- (转) Spring源码阅读 之 Spring整体架构
标签(空格分隔): Spring 声明:本文系转载,原地地址:spring framework 4 源码阅读 Spring骨架 Spring的骨架,也是Spring的核心包.主要包含三个内容 cont ...
- Spring源码阅读-ApplicationContext体系结构分析
目录 继承层次图概览 ConfigurableApplicationContext分析 AbstractApplicationContext GenericApplicationContext Gen ...
- Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)
我们到底能走多远系列(33) 扯淡: 各位: 命运就算颠沛流离 命运就算曲折离奇 命运就算恐吓着你做人没趣味 别流泪 心酸 更不应舍弃 ... 主题: Spring源码阅读还在继 ...
- 初始化IoC容器(Spring源码阅读)
初始化IoC容器(Spring源码阅读) 我们到底能走多远系列(31) 扯淡: 有个问题一直想问:各位你们的工资剩下来会怎么处理?已婚的,我知道工资永远都是不够的.未婚的你们,你们是怎么分配工资的? ...
- Sping学习笔记(一)----Spring源码阅读环境的搭建
idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...
- Spring源码阅读笔记02:IOC基本概念
上篇文章中我们介绍了准备Spring源码阅读环境的两种姿势,接下来,我们就要开始探寻这个著名框架背后的原理.Spring提供的最基本最底层的功能是bean容器,这其实是对IoC思想的应用,在学习Spr ...
- Spring源码阅读 之 配置的读取,解析
在上文中我们已经知道了Spring如何从我们给定的位置加载到配置文件,并将文件包装成一个Resource对象.这篇文章我们将要探讨的就是,如何从这个Resouce对象中加载到我们的容器?加载到容器后又 ...
- 搭建 Spring 源码阅读环境
前言 有一个Spring源码阅读环境是学习Spring的基础.笔者借鉴了网上很多搭建环境的方法,也尝试了很多,接下来总结两种个人认为比较简便实用的方法.读者可根据自己的需要自行选择. 方法一:搭建基础 ...
- Spring源码阅读总结(Ing)
一.Spring源码架构 Spring源码地址 二.Spring中的设计模式 1.工厂模式 BeanFactory 2.模板模式 模板的使用者只需设计一个具体的类,集成模板类,然后定制那些具体方法,这 ...
随机推荐
- Android 视频展示控件之 SurfaceView、GLSurfaceView、SurfaceTexture、TextureView 对比总结
一.SurfaceView SurfaceView继承自View,并提供了一个独立的绘图层,你可以完全控制这个绘图层,比如说设定它的大小,所以SurfaceView可以嵌入到View结构树中,需要注意 ...
- log4net应用实践(一)
1.背景 log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具.它是.Net下一 ...
- Unity 网格合并
从优化角度,Mesh需要合并. 从换装的角度(这里指的是换形状.换组成部件的换装,而不是挂点型的换装),都需要网格合并.材质合并.如果是人物的换装,那么需要合并SkinnedMeshRenderer, ...
- python爬微信公众号前10篇历史文章(3)-lxml&xpath初探
理解lxml以及xpath 什么是lxml? python中用来处理XML和HTML的library.与其他相比,它能提供很好的性能, 并且它支持XPath. 具体可以查看官方文档->http: ...
- python学习03-数据类型
一.基本数据类型--数字 布尔型 bool型只有两个值:True和False 之所以将bool值归类为数字,是因为我们也习惯用1表示True,0表示False. 以下是布尔值是False的各种情况: ...
- 【Zabbix】大规模监控误报发生时的处理方案
今天遇到了这样一件事..Zabbix一直在用的数据库突然间崩溃,无法连接了.在down掉的那一时刻开始,zabbix向管理员报了警.然后随之而来的是铺天盖地的所有主机zabbix agent无法连接的 ...
- java 对象和封装
软件出现的目的 面向对象设计和开发程序的好处用计算机语言描述现实世界 交流更加流畅用计算机解决现实世界的问题 提高设计和开发效率 面向对象的思想 描述→ 面向对象的世界 ...
- mac上Pycharm个性化快捷键,类似Myeclipse的快速复制等快捷键
好几天没写博客了,在win10下写了几天python,然后下了pycharm使用,发现还可以,但是太笨重了,切回了mac,然后装了pycharm社区版本. 但是这个使用太别扭了,没有myeclipse ...
- python 函数基础2
一.什么是命名关键字参数? 格式: 在*后面参数都是命名关键字参数. 特点:1.约束函数的调用者必须按照Kye=value的形式传值. 2,.约束函数的调用者必须用我们指定的Key名. def aut ...
- lua向文件中写入数据,进行记录
function readfile(path) local file = io.open(path, "r") if file then local content = file: ...