Spring IOC源码(二):IOC容器之 刷新前的准备
1、源码解析

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、应用
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、执行结果


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)

校验失败,抛异常,校验流程分析如下
Spring IOC源码(二):IOC容器之 刷新前的准备的更多相关文章
- Spring Ioc源码分析系列--容器实例化Bean的四种方法
Spring Ioc源码分析系列--实例化Bean的几种方法 前言 前面的文章Spring Ioc源码分析系列--Bean实例化过程(二)在讲解到bean真正通过那些方式实例化出来的时候,并没有继续分 ...
- 精尽Spring MVC源码分析 - WebApplicationContext 容器的初始化
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- Spring MVC源码(二) ----- DispatcherServlet 请求处理流程 面试必问
前端控制器 前端控制器,即所谓的Front Controller,体现的是设计模式中的前端控制器模式.前端控制器处理所有从用户过来的请求.所有用户的请求都要通过前端控制器.SpringMVC框架和其他 ...
- SSH 之 Spring的源码(二)——Bean实例化
首先来看一段代码,看过上一节的朋友肯定对这段代码并不陌生.这一段代码诠释了Spring加载bean的完整过程,包括读取配置文件,扫描包,加载类,实例化bean,注入bean属性依赖. <span ...
- 看看Spring的源码(二)——bean实例化
首先来看一段代码,看过上一节的朋友肯定对这段代码并不陌生.这一段代码诠释了Spring加载bean的完整过程,包括读取配置文件,扫描包,加载类,实例化bean,注入bean属性依赖. public v ...
- 精尽Spring MVC源码分析 - 文章导读
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- Spring 源码剖析IOC容器(一)概览
目录 一.容器概述 二.核心类源码解读 三.模拟容器获取Bean ======================= 一.容器概述 spring IOC控制反转,又称为DI依赖注入:大体是先初始化bean ...
- Spring Ioc源码分析系列--Bean实例化过程(二)
Spring Ioc源码分析系列--Bean实例化过程(二) 前言 上篇文章Spring Ioc源码分析系列--Bean实例化过程(一)简单分析了getBean()方法,还记得分析了什么吗?不记得了才 ...
- Spring源码解析-ioc容器的设计
Spring源码解析-ioc容器的设计 1 IoC容器系列的设计:BeanFactory和ApplicatioContext 在Spring容器中,主要分为两个主要的容器系列,一个是实现BeanFac ...
- Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析
Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析 前言 上一篇文章Spring Ioc源码分析系列--Ioc源码入口分析已经介绍到Ioc容器 ...
随机推荐
- 初试 Centos7 上 Ceph 存储集群搭建
转载自:https://cloud.tencent.com/developer/article/1010539 1.Ceph 介绍 Ceph 是一个开源的分布式存储系统,包括对象存储.块设备.文件系统 ...
- #Lua:Lua调用C++生成的DLL库
Lua调用C++生成的DLL库 本文参考了某大佬的博客,写得十分详细,推荐!!! 需求: 在之前的求解器中添加了Lua库,使得程序可以在Lua脚本中实现自定义函数功能,考虑到未来可能需要与第三方程序库 ...
- STM32F0单片机基于Hal库温控智能风扇
一.项目概述 设计采用STM32F0系列单片机做主控芯片,通过DHT11采集温湿度,将温度显示在OLED 屏幕上.根据温度的不同,利用STM32对风扇进行调速,总体硬件设计如下图所示 1.效果展示 2 ...
- Opengl ES之纹理贴图
纹理可以理解为一个二维数组,它可以存储大量的数据,这些数据可以发送到着色器上.一般情况下我们所说的纹理是表示一副2D图,此时纹理存储的数据就是这个图的像素数据. 所谓的纹理贴图,就是使用Opengl将 ...
- sql语句优化小结
sql的优化技巧. 1.用join进行子查询的优化. 低效的子查询 select a.user_name,a.over,(select over from user2 b where a.user_n ...
- Linux 下搭建 Hive 环境
Linux 下搭建 Hive 环境 作者:Grey 原文地址: 博客园:Linux 下搭建 Hive 环境 CSDN:Linux 下搭建 Hive 环境 前置工作 首先,需要先完成 Linux 下搭建 ...
- 2.ElasticSearch系列之集群权限认证
1. 在master节点上创建秘钥库 export ES_PATH_CONF="/home/elasticsearch/config" && /usr/local/ ...
- 微信小程序专题(二)-----微信openid的获取
一,简单来讲就是以下流程 通过get方式获取信息 在前端调用wx.login() 获取 临时登录凭证code之后,将code字符串发送给后端,后端通过auth.code2Session接口获取用户唯一 ...
- JUC(8)JMM
文章目录 1.JMM 2.volatile 3.单例模式 1.JMM Volatile是java虚拟机提供轻量级的同步机制 1.保证可见性 2.不保证原子性 3.禁止指令重排 什么是JMM java内 ...
- 实现etcd服务注册与发现
转载自:实现etcd服务注册与发现 0.1.目录结构 . ├── api │ └── main.go ├── common │ └── common.go ├── docker-compose ...