1、源码解析

  prepareRefresh()容器刷新refresh()的第一个方法,是容器刷新前的准备工作。
 1 // 容器启动的开始时间  毫秒级
2 private long startupDate;
3 // 容器目前是否活跃的标记
4 private final AtomicBoolean active = new AtomicBoolean();
5 // 当前容器是否已经被关闭的标记
6 private final AtomicBoolean closed = new AtomicBoolean();
7 // 容器刷新前注册进来的监听器集合
8 private Set<ApplicationListener<?>> earlyApplicationListeners;
9 // 容器的监听器集合
10 private final Set<ApplicationListener<?>> applicationListeners = new LinkedHashSet<>();
11 // 要发布的应用程序事件集合(在多播器未设置之前要发布的事件)
12 private Set<ApplicationEvent> earlyApplicationEvents;
13
14 // 刷新容器前的准备工作
15 protected void prepareRefresh() {
16 // Switch to active.
17 // 记录容器启动时间
18 this.startupDate = System.currentTimeMillis();
19 // 设置关闭状态为false
20 this.closed.set(false);
21 // 设置活跃状态为true
22 this.active.set(true);
23
24 // 日志记录
25 if (logger.isDebugEnabled()) {
26 if (logger.isTraceEnabled()) {
27 logger.trace("Refreshing " + this);
28 }
29 else {
30 logger.debug("Refreshing " + getDisplayName());
31 }
32 }
33
34 // 具体实现由子类完成,初始化属性资源
35 initPropertySources();
36
37 // 获取Environment对象,并验证当前系统需要的属性值是否都加载到了Environment对象中
38 getEnvironment().validateRequiredProperties();
39
40 // 准备监听器和事件的集合对象,默认为空的集合
41 if (this.earlyApplicationListeners == null) {
42 this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
43 }
44 else {
45 // 重置本地应用监听器为预刷新状态,清空集合缓存
46 this.applicationListeners.clear();
47 // 将容器刷新前的监听器与事件集合对象设置到当前容器监听器集合中
48 this.applicationListeners.addAll(this.earlyApplicationListeners);
49 }
50
51 // 创建刷新前的事件集合,依赖于多播器,一旦多播器是可用的,事件集合中的事件就可以被发布
52 this.earlyApplicationEvents = new LinkedHashSet<>();
53 }

综上可见,容器刷新前的准备工作prepareRefresh()完成以下事情:

  1、记录容器启动时间

  2、设置当前容器关闭状态为false
  3、设置当前容器活跃状态为true
  4、设置拓展点,初始化资源占位符
  5、设置环境Environment对象,并将当前系统属性值加载至环境对象中
  6、监听器、事件集合的处理 - Spring框架提供的扩展点,在SpringBoot中有应用。

2、应用

  Spring在prepareRefresh()有对初始化资源占位符initPropertySources()的拓展点,下面我们来看看如何实现拓展。
  initPropertySources()方法是AbstractApplicationContext中的待子类实现的方法。
protected void initPropertySources() {
// For subclasses: do nothing by default.
}

  从IOC源码(一):IOC容器启动流程核心方法概览中ClassPatchXmlApplicationContext类图中我们得知,ClassPatchXmlApplicationContext是AbstractApplicationContext的子类,下面我们来看看初始化资源占位符的具体实现。

2.1、定义配置文件 - snailsInitPropertySource.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
7
8 </beans>

2.2、拓展初始化属性资源,重写initPropertySources()方法

 1 import org.springframework.beans.BeansException;
2 import org.springframework.context.support.ClassPathXmlApplicationContext;
3
4 // 初始化属性资源的拓展实现
5 public class SnailsInitApplicationContext extends ClassPathXmlApplicationContext {
6
7 public SnailsInitApplicationContext(String... configLocations) throws BeansException {
8 super(configLocations);
9 }
10
11 @Override
12 protected void initPropertySources() {
13 System.out.println("expand initPropertySource... ");
14 // 添加属性到Environment环境中
15 getEnvironment().getSystemProperties().put("address","中国");
16 // Environment环境中的必输属性,会通过validateRequiredProperties方法做校验
17 getEnvironment().setRequiredProperties("addr");
18 }
19 }

2.3、测试代码

 1 /**
2 * @Description: 测试拓展初始化资源属性拓展
3 * @author: snails
4 * @since: 2022/3/1 11:02
5 */
6 public class TestSnailsInitApplicationContext {
7 public static void main(String[] args) {
8 SnailsInitApplicationContext context = new SnailsInitApplicationContext("snailsInitPropertySource.xml");
9 }
10 }

2.4、执行结果

  getEnvironment().getSystemProperties().put("address","中国");
  address属性已经加载到environment#propertySources#propertySourceList集合中name为systemProperties的 source集合中,source中存储的是Properties对象。
  getEnvironment().setRequiredProperties("addr");
Exception in thread "main" org.springframework.core.env.MissingRequiredPropertiesException: The following properties were declared as required but could not be resolved: [addr]
at org.springframework.core.env.AbstractPropertyResolver.validateRequiredProperties(AbstractPropertyResolver.java:145)
at org.springframework.core.env.AbstractEnvironment.validateRequiredProperties(AbstractEnvironment.java:519)
at org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:646)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:153)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:97)
at selfInitPropertySources.SnailsInitApplicationContext.<init>(SnailsInitApplicationContext.java:14)
at selfInitPropertySources.TestSnailsInitApplicationContext.main(TestSnailsInitApplicationContext.java:11)

校验失败,抛异常,校验流程分析如下

  1、将要校验的属性addr加入校验集合requiredProperties中;
  2、AbstractPropertyResolver#validateRequiredProperties()校验,遍历校验集合requiredProperties,通过key=addr从environment中的source集合中获取value;
  3、若通过key获取的value为空,抛出异常。

Spring IOC源码(二):IOC容器之 刷新前的准备的更多相关文章

  1. Spring Ioc源码分析系列--容器实例化Bean的四种方法

    Spring Ioc源码分析系列--实例化Bean的几种方法 前言 前面的文章Spring Ioc源码分析系列--Bean实例化过程(二)在讲解到bean真正通过那些方式实例化出来的时候,并没有继续分 ...

  2. 精尽Spring MVC源码分析 - WebApplicationContext 容器的初始化

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  3. Spring MVC源码(二) ----- DispatcherServlet 请求处理流程 面试必问

    前端控制器 前端控制器,即所谓的Front Controller,体现的是设计模式中的前端控制器模式.前端控制器处理所有从用户过来的请求.所有用户的请求都要通过前端控制器.SpringMVC框架和其他 ...

  4. SSH 之 Spring的源码(二)——Bean实例化

    首先来看一段代码,看过上一节的朋友肯定对这段代码并不陌生.这一段代码诠释了Spring加载bean的完整过程,包括读取配置文件,扫描包,加载类,实例化bean,注入bean属性依赖. <span ...

  5. 看看Spring的源码(二)——bean实例化

    首先来看一段代码,看过上一节的朋友肯定对这段代码并不陌生.这一段代码诠释了Spring加载bean的完整过程,包括读取配置文件,扫描包,加载类,实例化bean,注入bean属性依赖. public v ...

  6. 精尽Spring MVC源码分析 - 文章导读

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  7. Spring 源码剖析IOC容器(一)概览

    目录 一.容器概述 二.核心类源码解读 三.模拟容器获取Bean ======================= 一.容器概述 spring IOC控制反转,又称为DI依赖注入:大体是先初始化bean ...

  8. Spring Ioc源码分析系列--Bean实例化过程(二)

    Spring Ioc源码分析系列--Bean实例化过程(二) 前言 上篇文章Spring Ioc源码分析系列--Bean实例化过程(一)简单分析了getBean()方法,还记得分析了什么吗?不记得了才 ...

  9. Spring源码解析-ioc容器的设计

    Spring源码解析-ioc容器的设计 1 IoC容器系列的设计:BeanFactory和ApplicatioContext 在Spring容器中,主要分为两个主要的容器系列,一个是实现BeanFac ...

  10. Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析

    Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析 前言 上一篇文章Spring Ioc源码分析系列--Ioc源码入口分析已经介绍到Ioc容器 ...

随机推荐

  1. linux系统排查数据包常用命令

    1.查看当前系统中生效的所有参数 sysctl -a 2.统计处于TIME_WAIT状态的TCP连接数 netstat -ant|grep TIME_WAIT|wc -l 3.统计TCP连接数 net ...

  2. win7通过netsh命令禁用、启用本地连接 定时关闭开启网络连接

    1) 先检查网络接口名称 C:\Windows\system32>netsh interface show interface 管理员状态 状态 类型 接口名称 ---------------- ...

  3. MySQL数据库安装保姆教程及问题解决

    使用Mysql的zip压缩包解压版,下载之后需进行一定的配置,才能使用它. 下面对Mysql压缩包版的安装方法进行详细的描述,如有疑问或错误,望及时反馈. 首先,mysql的官方下载地址点我进行下载 ...

  4. [算法2-数组与字符串的查找与匹配] (.NET源码学习)

    [算法2-数组与字符串的查找与匹配] (.NET源码学习) 关键词:1. 数组查找(算法)   2. 字符串查找(算法)   3. C#中的String(源码)   4. 特性Attribute 与内 ...

  5. while.for循环和基本数据类型内置方法

    while循环补充说明 流程控制之for循环 基本数据类型内置方法 内容详细 1.死循环 真正的死循环是一旦执行,Cpu的功耗会急速上升 知道系统采取紧急措施 所以 尽量不要让cpu长时间不断运算. ...

  6. 图片 css剪切,等比例缩放

    object-fit: cover; .img1 {//自定义图片宽高,并且等比例缩放 width: 200px; height: 400px; object-fit: cover; }

  7. POJ2763 Housewife Wind (树链剖分)

    差不多是模板题,不过要注意将边权转化为点权,将边的权值赋给它所连的深度较大的点. 这样操作过后,注意查询ask()的代码有所改变(见代码注释) 1 #include<cstdio> 2 # ...

  8. 通过netty把百度地图API获取的地理位置从Android端发送到Java服务器端

    本篇记录我在实现时的思考过程,写给之后可能遇到困难的我自己也给到需要帮助的人. 写的比较浅显,见谅. 在写项目代码的时候,需要把Android端的位置信息传输到服务器端,通过Netty达到连续传输的效 ...

  9. 1、小程序Vant_WebApp组件库的安装步骤和简单使用

    Vant 1.小程序对于npm的支持 目前,小程序当中已经支持使用npm安装的第三方包,通过使用这些第三方包,我们可以提高对小程序开发的效率,但是在小程序当中使用所谓的npm包有如下的三个限制 不能支 ...

  10. cmd复制移动合并文件

    1.单文件复制: (1)把c:\1.txt复制到c:\2\文件夹下 copy c:\1.txt c:\2\ (2)把c:\1.txt复制到c:\1\文件夹下,并把它修改为777.docx copy c ...