03.Spring IoC 容器 - 初始化
基本概念
Spring IoC 容器的初始化过程在监听器 ContextLoaderListener 类中定义。
具体由该类的的 configureAndRefreshWebApplicationContext 方法实现,它包含了两个过程:
- 配置过程
- 刷新过程
原理分析
下面来看 configureAndRefreshWebApplicationContext 方法的具体实现:
// 表示容器的标识
public static final String CONTEXT_ID_PARAM = "contextId";
// 表示容器的配置文件路径
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
protected void configureAndRefreshWebApplicationContext(
ConfigurableWebApplicationContext wac, ServletContext sc) {
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
// 配置过程:
// 1.设置容器的标识,即 ContextId
String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
if (idParam != null) {
wac.setId(idParam);
}else {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(sc.getContextPath()));
}
}
// 2.设置容器的 ServletContext
wac.setServletContext(sc);
// 3.设置容器的配置文件路径,即 ContextConfigLocation
String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
wac.setConfigLocation(configLocationParam);
}
// 4.设置容器的环境,并初始化它的属性
ConfigurableEnvironment env = wac.getEnvironment();
// 5.初始化容器的环境属性
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
}
// 自定义过程,暂不探究
customizeContext(sc, wac);
// 刷新过程:
wac.refresh();
}
Spring 容器的初始化过程实际被细分为了两个过程:配置过程、刷新过程
在 ContextLoaderListener 类中主要完成了配置过程,即设置容器的 ContextId,ServletContext,ConfigLocation,ConfigurableEnvironment 属性等。
刷新的过程则由刚刚创建 Spring 容器自己完成。
配置过程
1.设置容器的环境
Spring 容器在设置的它的 Environment 属性时,如果不存在则默认创建一个 StandardServletEnvironment对象。具体的继承关系如下:
来看下 getEnvironment 方法:
private ConfigurableEnvironment environment;
public ConfigurableEnvironment getEnvironment() {
// 不存在,则创建
if (this.environment == null) {
this.environment = createEnvironment();
}
return this.environment;
}
protected ConfigurableEnvironment createEnvironment() {
return new StandardServletEnvironment();
}
再来分析 StandardEnvironment 的初始化过程,该类在初始化过程中,会创建一个 propertySources 对象来保存系统相关的环境变量与属性。
// AbstractEnvironment 类
private final MutablePropertySources propertySources =
new MutablePropertySources(this.logger);
public AbstractEnvironment() {
customizePropertySources(this.propertySources);
// 省略部分代码...
}
// StandardEnvironment 类
public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME ="systemEnvironment";
public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = "systemProperties";
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(
new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
getSystemProperties()));
propertySources.addLast(
new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
getSystemEnvironment()));
}
2.初始化容器的环境属性
即初始化 Environment 的 propertySources 属性,它会将 ServletContext 、ServletConfig 添加到
propertySources 中。
// StandardServletEnvironment 类
public void initPropertySources(ServletContext servletContext,
ServletConfig servletConfig) {
// 将 servletContext、servletConfig 添加到 propertySources
WebApplicationContextUtils.initServletPropertySources(
getPropertySources(),servletContext, servletConfig);
}
public MutablePropertySources getPropertySources() {
return this.propertySources;
}
03.Spring IoC 容器 - 初始化的更多相关文章
- 整理在Spring IOC容器初始化后可以处理特定逻辑的多种实现方式
Spring框架的核心是依赖注入.切面:Spring Boot是在Spring框架的基础上为其提供许多默认配置.默认约定(约定优于配置),从而达到减少或减化配置进而可开箱即用.快速上手:Spring ...
- Spring IoC容器初始化过程学习
IoC容器是什么?IoC文英全称Inversion of Control,即控制反转,我么可以这么理解IoC容器: 把某些业务对象的的控制权交给一个平台或者框架来同一管理,这个同一管理的平台可以称为I ...
- Spring源码分析:Spring IOC容器初始化
概述: Spring 对于Java 开发来说,以及算得上非常基础并且核心的框架了,在有一定开发经验后,阅读源码能更好的提高我们的编码能力并且让我们对其更加理解.俗话说知己知彼,百战不殆.当你对Spri ...
- Spring Ioc 容器初始化过程
IOC 是如何工作的? 通过 ApplicationContext 创建 Spring 容器,容器读取配置文件 "/beans.xml" 并管理定义的 Bean 实例对象. 通 ...
- springmvc web.xml配置之 -- SpringMVC IOC容器初始化
SpringMVC IOC容器初始化 首先强调一下SpringMVC IOC容器初始化有些特别,在SpringMVC中除了生成一个全局的spring Ioc容器外,还会为DispatcherServl ...
- JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(6):Spring IOC容器学习(概念、作用、Bean生命周期)
一.IOC控制反转概念 控制反转(IOC)是一种通过描述(在Java中可以是XML或者是注解)并通过第三方去生产或获取特定对象的方式. 主动创建模式,责任在于开发者,而在被动模式下,责任归于Ioc容器 ...
- Spring IoC容器的初始化过程
Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...
- spring源码学习之路---深度分析IOC容器初始化过程(四)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 最近由于工作和生活,学习耽搁 ...
- spring源码 — 一、IoC容器初始化
IoC容器初始化 注意:本次的spring源码是基于3.1.1.release版本 容器:具有获取Bean功能--这是最基本功能,也是BeanFactory接口定义的主要行为,在添加了对于资源的支持之 ...
随机推荐
- Operating System-Thread(2) Multi-Process-Parallel vs Multi-Thread-Parallel
本文主要介绍线程的模型 一.Multi-Process-Parallel vs Multi-Thread-Parallel 多进程的并行:CPU在多个进程之间进行切换,系统给人一种多个进程在并行执行的 ...
- 杂项-Log:log4net
ylbtech-杂项-Log:log4net log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等) ...
- 3 K8s安裝ELK+filebeat
1 Filebeat: apiVersion: v1 kind: Service metadata: name: XX spec: ports: - name: http port: targetPo ...
- ES6学习之Proxy
定义:“代理器”,用于修改某些操作的默认行为,等同于在语言层面做出修改,所以属于一种“元编程”(meta programming),即对编程语言进行编程.可以对外界的访问进行过滤和改写. 语法: va ...
- Math类简介
Math abs max min 分别是绝对值 最大值,最小值 round 四舍五入 ceil ceil(32.6) 33.0 ceil(32.2) 33.0 返回大于该数值的较大的整数 与之相对 ...
- 机器学习前沿热点——Deep Learning
深度学习是机器学习研究中的一个新的领域,其动机在于建立.模拟人脑进行分析学习的神经网络,它模仿人脑的机制来解释数据,例如图像.声音和文本.深度学习是无监督学习的一种. 深度学习的概念源于人工神经网络的 ...
- 02_android下单元测试
Java的单元测试JUnit. Java程序入口是main方法.一般不在安卓程序入口 @Override protected void onCreate(Bundle savedInstanceSta ...
- 9、par画图参数
转载:http://blog.sina.com.cn/s/blog_8f5b2a2e0102v0tf.html 1. 函数par()的使用格式如下: par(..., no.readonly = FA ...
- 《精通Spring4.X企业应用开发实战》读后感第七章(创建增强类)
- 292C Beautiful IP Addresses
传送门 题目 The problem uses a simplified TCP/IP address model, please read the statement carefully. An I ...