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容器 ...
随机推荐
- Kubernetes Node的隔离与恢复
在硬件升级.硬件维护等情况下,我们需要将某些Node进行隔离,脱离Kubernetes集群的调度范围.Kubernetes提供了一种机制,即可以将Node纳入调度范围,也可以将Node脱离调度范围. ...
- Compass- 图形化界面客户端
到MongoDB官网下载MongoDB Compass, 地址: https://www.mongodb.com/download-center/v2/compass?initial=true 如果是 ...
- 【前端必会】单页应用-你的新朋友wepack
背景 我们开发的功能可能是简单的,但是实现功能的代码行数却可能成千上万 出于易于维护.安全.服用,我们会根据我们的经验设计我们的代码,拆解成多个独立的功能模块(代码片段.更多的文件) JS的模块规范有 ...
- python之流程控制上-if、while
流程控制 编写程序,是将自己的逻辑思想记录下来,使得计算机能够执行的过程. 而流程控制,则是逻辑结构中十分重要的一环. 在程序中,基础的流程结构分为顺序结构.分支结构.顺序结构 顺序结构自不必多说,上 ...
- Docker 部署 Kibana
Docker 部署 Kibana 本篇主要介绍 使用 Docker 部署 kibana 用于操作 Elasticsearch 使用. 1. 前置准备 1.1 Elasticsearch 准备 可以先准 ...
- Day3 最短路 最小生成树 拓扑排序
Day3 最短路 最小生成树 拓扑排序 (一)最短路 一.多源最短路 从任意点出发到任意点的最短路 1. Floyd \(O(n^3)\) for(int k=1;k<=n;k++) for(i ...
- Linux基础_1_简介
Linux是什么 一款优秀的操作系统软件,特性是一切皆文件:一切设备皆文件!一切设备的设置皆修改配置文件!一切服务的搭建皆修改配置文件!(庞大的树形结构文件系统) 根据FHS标准,Linux目录有以下 ...
- Vue前端框架基础+Element的使用
前置内容: AJAX基础+Axios快速入门+JSON使用 目录 1.VUE 1.1 概述 1.2 快速入门 1.3 Vue指令 1.3.1 v-bind & v-model 指令 1.3.2 ...
- Oracle中新建数据表的两种方法
首发微信公众号:SQL数据库运维 原文链接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485212&idx=1 ...
- C#文件路径
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...