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. Bootstrap5 如何创建多媒体对象

    一.在Bootstra5中使用媒体对象 Bootstrap 媒体对象在版本 5 中已经停止支持了.但是,我们仍然可以使用 flex 和 margin 创建包含左对齐或右对齐媒体对象(如图像或视频)以及 ...

  2. 洛谷U81904 【模板】树的直径

    有负边权,所以用树形DP来找树的直径. 1 //树形DP求树的直径 2 #include<bits/stdc++.h> 3 using namespace std; 4 const int ...

  3. 基于纯前端类Excel表格控件实现在线损益表应用

    财务报表也称对外会计报表,是会计主体对外提供的反映企业或预算单位一定时期资金.利润状况的会计报表,由资产负债表.损益表.现金流量表或财务状况变动表.附表和附注构成.财务报表是财务报告的主要部分,不包括 ...

  4. 虚拟化_Vmware——敬请期待!

    Esxi 7.0 安装部署完成! vSphere vCenter 安装部署完成!

  5. Codeforces1695 D1.+D2 Tree Queries

    题意 给一个n个点的无向图,其中有一个隐藏点X,可以进行一组询问S来确定S是n个节点中的哪个点.S包括k个询问节点.询问返回的值也为k个值,每个值为X点到每个询问节点的最短路距离,求k最小为多少. 提 ...

  6. 深入浅出redis缓存应用

    0.1.索引 https://blog.waterflow.link/articles/1663169309611 1.只读缓存 只读缓存的流程是这样的: 当查询请求过来时,先从redis中查询数据, ...

  7. SpringCloud微服务实战——搭建企业级开发框架(四十七):【移动开发】整合uni-app搭建移动端快速开发框架-添加Axios并实现登录功能

      uni-app自带uni.request用于网络请求,因为我们需要自定义拦截器等功能,也是为了和我们后台管理保持统一,这里我们使用比较流行且功能更强大的axios来实现网络请求.   Axios ...

  8. 京东云开发者|京东云RDS数据迁移常见场景攻略

    云时代已经来临,云上很多场景下都需要数据的迁移.备份和流转,各大云厂商也大都提供了自己的迁移工具.本文主要介绍京东云数据库为解决用户数据迁移的常见场景所提供的解决方案. 场景一:数据迁移上云 数据迁移 ...

  9. 空链接的作用以及<a href="#"></a>和<a href="javascript:;"></a>的区别

    空链接的作用以及<a href="#"></a>和<a href="javascript:;"></a>的区别在 ...

  10. CSS 动画一站式指南

    CSS 动画一站式指南 目录 CSS 动画一站式指南 1. CSS 动画 1.1 变换 1.1.1 变换属性介绍 1.1.2 变换动画实践 1.2 过渡 1.2.1 过渡属性介绍 1.2.2 过渡动画 ...