专题五:Spring源码之初始化容器上下文

上一篇我们通过如下一段基础代码作为切入点,最终找到核心的处理是refresh方法,从今天开始正式进入refresh方法的解读。

public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");JmUser jmUser = (JmUser)context.getBean("jmUser");System.out.println(jmUser.getName());System.out.println(jmUser.getAge());}
}

初始化容器上下文

首先还是整体看下refresh方法

	@Overridepublic void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing. 1、初始化上下文信息,替换占位符、必要参数的校验prepareRefresh();// Tell the subclass to refresh the internal bean factory. 2、解析类Xml、初始化BeanFactoryConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 这一步主要是对初级容器的基础设计// Prepare the bean factory for use in this context. 	3、准备BeanFactory内容:prepareBeanFactory(beanFactory); // 对beanFactory容器的功能的扩展:try {// Allows post-processing of the bean factory in context subclasses. 4、扩展点加一:空实现,主要用于处理特殊Bean的后置处理器postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context. 	5、spring bean容器的后置处理器invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation. 	6、注册bean的后置处理器registerBeanPostProcessors(beanFactory);// Initialize message source for this context.	7、初始化消息源initMessageSource();// Initialize event multicaster for this context.	8、初始化事件广播器initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses. 9、扩展点加一:空实现;主要是在实例化之前做些bean初始化扩展onRefresh();// Check for listener beans and register them.	10、初始化监听器registerListeners();// Instantiate all remaining (non-lazy-init) singletons.	11、实例化:非兰加载BeanfinishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.	 12、发布相应的事件通知finishRefresh();}catch (BeansException ex) {if (logger.isWarnEnabled()) {logger.warn("Exception encountered during context initialization - " +"cancelling refresh attempt: " + ex);}// Destroy already created singletons to avoid dangling resources.destroyBeans();// Reset 'active' flag.cancelRefresh(ex);// Propagate exception to caller.throw ex;}finally {// Reset common introspection caches in Spring's core, since we// might not ever need metadata for singleton beans anymore...resetCommonCaches();}}}

首先将目标聚焦在第一个方法prepareRefresh方法上,根据方法名称和注释,我们大概可以猜测到该方法是在容器初始化前做些准备工作。

有了这个想法我来具体看下这个方法到底干了什么?

/*** Prepare this context for refreshing, setting its startup date and* active flag as well as performing any initialization of property sources.* 一些初始化设置如:设置容器开始事件、容器状态active设置激活】初始化配置源等。* 1.1、其中关注初始化配置源:这个也是留给子类自己实现,扩展点加一* 1.2、容器初始化的时候,校验必须的配置是否为空,当我们自己对原框架修改的时候,可以通过这个属性加上必要的配置判断**/protected void prepareRefresh() {// Switch to active.this.startupDate = System.currentTimeMillis();this.closed.set(false);this.active.set(true);if (logger.isDebugEnabled()) {if (logger.isTraceEnabled()) {logger.trace("Refreshing " + this);}else {logger.debug("Refreshing " + getDisplayName());}}// Initialize any placeholder property sources in the context environment.// 初始化替换占位符为实际值initPropertySources();// Validate that all properties marked as required are resolvable:// see ConfigurablePropertyResolver#setRequiredProperties// 容器初始化的时候,校验必须的配置是否为空getEnvironment().validateRequiredProperties();// Store pre-refresh ApplicationListeners...if (this.earlyApplicationListeners == null) {this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);}else {// Reset local application listeners to pre-refresh state.this.applicationListeners.clear();this.applicationListeners.addAll(this.earlyApplicationListeners);}// Allow for the collection of early ApplicationEvents,// to be published once the multicaster is available...this.earlyApplicationEvents = new LinkedHashSet<>();}

可以看到,该方法大部分时间只是做了初始化的设置如开始时间、容器状态初始化等,聚焦下

initPropertySources方法
	/*** <p>Replace any stub property sources with actual instances.* @see org.springframework.core.env.PropertySource.StubPropertySource* @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources*/protected void initPropertySources() {// For subclasses: do nothing by default.}

Spring最经典的设计之一,空实现方法方法的权限级别为protected。给子类自己实现,扩展点加一。这里单独提出来和大家看看,因为后面我们能看到很多类似的代码。这也是Spring是一个易扩展框架的原因之一。

说完这个方法的设计,下面再来看看这个方法具体干了什么。看注释说是为了替换占位符。既然这样我们自己来重写这个方法试试看就知道啦。重写代码如下:

public class MyselfClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {/*** Create a new ClassPathXmlApplicationContext, loading the definitions* from the given XML file and automatically refreshing the context.* @param configLocations resource location* @throws BeansException if context creation failed*/public MyselfClassPathXmlApplicationContext(String... configLocations) throws BeansException {super(configLocations);}@Overrideprotected void initPropertySources() {System.out.println("自定义 initPropertySources");getEnvironment().getSystemProperties().put("systemOS", "mac");}public class Main {public static void main(String[] args) {ApplicationContext context = new MyselfClassPathXmlApplicationContext("applicationContext.xml");JmUser jmUser = (JmUser)context.getBean("jmUser");System.out.println(jmUser.getName());System.out.println(jmUser.getAge());}
}

执行完initPropertySources方法以后,发现环境变量多了我们设置的代码systemOS,后续在需要的地方可以替换成我们所需要的值。

接着我们来看prepareRefresh下一个方法:

getEnvironment().validateRequiredProperties();

老样子根据注释和方法名称简答猜测一下,应该是用来校验是否需要检验某个必须的属性。猜测后进入代码验证一波。

看代码是自己的成员属性propertyResolver进行调用的,在进入方法看下:

@Overridepublic void validateRequiredProperties() {MissingRequiredPropertiesException ex = new MissingRequiredPropertiesException();for (String key : this.requiredProperties) {if (this.getProperty(key) == null) {ex.addMissingRequiredProperty(key);}}if (!ex.getMissingRequiredProperties().isEmpty()) {throw ex;}}

上述代码主要是遍历requirePrpperties属性,将不存在的key存入ex中,待循环结束以后抛出异常。目前我们的代码属性为空。我们再改写下上述代码看看。

package org.springframework;import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author Jeremy* @version 1.0* @description: 自定义容器* @date 2024/7/1 20:17*/
public class MyselfClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {/*** Create a new ClassPathXmlApplicationContext, loading the definitions* from the given XML file and automatically refreshing the context.* @param configLocations resource location* @throws BeansException if context creation failed*/public MyselfClassPathXmlApplicationContext(String... configLocations) throws BeansException {super(configLocations);}@Overrideprotected void initPropertySources() {System.out.println("自定义 initPropertySources");
//		getEnvironment().getSystemProperties().put("systemOS", "mac");getEnvironment().setRequiredProperties("systemOS");}
}

堆栈日志如下

自定义 initPropertySources
Disconnected from the target VM, address: 'localhost:50403', transport: 'socket'
Exception in thread "main" org.springframework.core.env.MissingRequiredPropertiesException: The following properties were declared as required but could not be resolved: [systemOS]
    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:602)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:148)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:95)
    at org.springframework.MyselfClassPathXmlApplicationContext.<init>(MyselfClassPathXmlApplicationContext.java:20)
    at org.springframework.Main.main(Main.java:15)

放开上面注释:正常运行。通过上述两个简单的实例我们可以通过重写上述代码为我们Spring容器提供基础的校验和设置对应的值。方便后续开发。到这里我们初始化容器上下文prepareRefresh方法告一段落。

总结

目前我们代码进程如下图所示:

下一节我们正式进入初始化容器,看看众所周知的Bean Factory到底怎么来的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/37867.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

鸿蒙本地签名不匹配问题

连接鸿蒙手机运行项目报如下错误 这是由于本地签名和鸿蒙设备签名不匹配导致的&#xff0c;需要注释掉如下代码&#xff0c;选择file project 自动签名 勾选auto选项&#xff0c;会在build-profile.json5中生成一个签名&#xff0c;然后运行就ok了~

【Lua】脚本入门

文章目录 总述一、Lua概述二、Lua环境安装三、Lua基本语法四、Lua的库和扩展五、Lua的应用场景六、学习资源 语法1. Lua基本语法示例变量和数据类型控制结构函数 2. Lua标准库示例字符串操作数学函数文件I/O 3. Lua作为脚本扩展示例&#xff08;假设Lua嵌入在某个应用程序中&am…

vscode python格式化

插件 Black Formatter Black 默认会遵循 PEP 8 的规范&#xff0c;可配置的参数很少&#xff0c;用的人很多。 setting.json 配置&#xff0c;更改插件的每行字符数限制 {"[python]": {"editor.defaultFormatter": "ms-python.black-formatter"…

Redis命令大全(基础版)

一、基础命令 redis-server --service-start # 开启服务 redis-server --service-stop # 停止服务redis-cli # 进入redis界面redis界面操作&#xff1a; ping # 检测状态&#xff0c;返回pong证明连接正常set key value # 设置 key 字段的值为value&#xff0c;返回o…

创建一个Django用户认证系统

目录 1、Django2、Django用户认证系统User 模型&#xff1a;Authentication 视图&#xff1a;认证后端 (Authentication Backends)&#xff1a;Form 类&#xff1a;中间件 (Middleware)&#xff1a;权限和组 (Permissions and Groups)&#xff1a; 3、创建一个django用户认证系…

服务器的分类,主流服务器的应用场景

一、服务器分类 服务器可以按应用层次、体系架构、用途、外形等进行分类。以下是详细说明&#xff1a; 按应用层次分类 入门级服务器&#xff1a;这些服务器一般用于小型企业或部门的简单任务&#xff0c;如文件共享和打印服务。工作组级服务器&#xff1a;适用于中小型企业&…

html2canvas相关(生成图片)

根据 DOM 生成对应的图片 function export3png(row, type null) { html2canvas( document.querySelector(#bug), //要生成图片的dom节点 {useCORS: true, }) 图片跨域 .then((canvas) > { const saveUrl canvas.toDataURL(image/png) Canvas对象生成base64代码 co…

MNIST手写字体识别(算法基础)

快教程 10分钟入门神经网络 PyTorch 手写数字识别 慢教程 【深度学习Pytorch入门】 简单回归问题-1 梯度下降算法 梯度下降算法 l o s s x 2 ∗ s i n ( x ) loss x^2 * sin(x) lossx2∗sin(x) 求导得&#xff1a; f ‘ ( x ) 2 x s i n x x 2 c o s x f^(x)2xsinx x^…

uORF和non-overlap对翻译效率的影响

以下是重叠和非重叠上游开放阅读框&#xff08;uORFs&#xff09;对翻译效率影响的总结&#xff1a; 重叠uORFs&#xff1a; 重叠uORFs对主要编码区的翻译影响更为显著&#xff0c;因为它们直接与下游编码序列&#xff08;CDSs&#xff09;竞争核糖体结合。重叠uORFs的翻译起始…

在C++程序中嵌入quickjs实现C++和javascript互相调用

quickjs是一个C实现的轻量级javascript解析引擎&#xff0c;可以嵌入到C程序中&#xff0c;实现C和js代码的交互。 以下基于quickjs-ng这一社区分支实现样例代码演示利用quickjs编写程序进行C和js互相调用&#xff0c;支持linux和windows。 代码结构 quickjs_demo- quickjs-…

Cesium大屏-vue3注册全局组件

1.需求 说明&#xff1a;产品经理要求开发人员在地图大屏上面随意放置组件&#xff0c;并且需要通过数据库更改其组件大小&#xff0c;位置等&#xff1b;适用于大屏组件中场站视角、任意位置标题等。 2.实现 2.1GlobalComponents.vue 说明&#xff1a;containerList可以通…

python基础语法 004-2流程控制- for遍历

1 遍历 1.1 什么是遍历&#xff1f; 可以遍历的元素&#xff1a;字符串、列表、元组、字典、集合字符串是可以进行for 循环。&#xff08;容器对象&#xff0c;序列&#xff09;可迭代对象iterable 例子&#xff1a; 1 &#xff09;、for遍历字符串&#xff1a; name xiao…

RK3568驱动指南|第十五篇 I2C-第167章 I2C上拉电阻

瑞芯微RK3568芯片是一款定位中高端的通用型SOC&#xff0c;采用22nm制程工艺&#xff0c;搭载一颗四核Cortex-A55处理器和Mali G52 2EE 图形处理器。RK3568 支持4K 解码和 1080P 编码&#xff0c;支持SATA/PCIE/USB3.0 外围接口。RK3568内置独立NPU&#xff0c;可用于轻量级人工…

SpringBoot实现图片添加水印

提示&#xff1a;今日完成图片添加水印功能 后续可能还会继续完善这个功能 文章目录 目录 文章目录 前端部分 后端 Xml Controller层 Sercive层 Service实现层 Config配置层 application.properties 文件后缀名获取 常量定义 前端部分 <!DOCTYPE html> <htm…

WIN11,如何同时连接有线网络与WLAN无线网络

之前写了两篇文章&#xff0c;一篇是双网卡多网卡时win11如何设置网卡优先级_多网卡设置网卡优先级-CSDN博客 另一篇是win11 以太网和WLAN冲突 连接网线时导致WiFi掉线 解决_win11 以太网和wifi不能同时生效-CSDN博客 这篇是对上面两篇的补充&#xff1a;主要解决电脑重启后&…

语音芯片TD5580,USB小音响芯片—拓达半导体

有时候电脑的声卡会出现损坏的问题&#xff0c;给我们的生活带来了很多麻烦。这时候&#xff0c;我们就需要一款方便易用的产品来解决声卡问题。USB声卡小音响就是为了解决这个问题而设计的一款便捷的产品。它不仅可以作为一个小音响&#xff0c;让您在工作和娱乐的时候享受高品…

docker-compose搭建minio对象存储服务器

docker-compose搭建minio对象存储服务器 最近想使用oss对象存储进行用户图片上传的管理&#xff0c;了解了一下例如aliyun或者腾讯云的oss对象存储服务&#xff0c;但是呢涉及到对象存储以及经费有限的缘故&#xff0c;决定自己手动搭建一个oss对象存储服务器&#xff1b; 首先…

烧结银到底有多牛?欢迎咨询SHAREX善仁新材研究院

烧结银到底有多牛&#xff1f;欢迎咨询SHAREX善仁新材研究院 在当今日新月异的科技浪潮中&#xff0c;材料科学以其独特的魅力引领着人类探索未知领域的步伐。在众多前沿材料中&#xff0c;烧结银凭借其卓越的性能和广泛的应用前景&#xff0c;逐渐崭露头角&#xff0c;成为科…

创建XCOM窗体和跳转连接

Xcom 窗体&#xff1a; (groupBox组合框&#xff0c;comboBox下拉框) xcom代码&#xff1a; namespace _01_作业 {// 1kb 1024B 1200B// 1MB public partial class Form1 : Form{public List<string> botelv new List<string> { "600","1200&…

Unix Network Programming Episode 96

‘socketpair’ Function The socketpair function creates two sockets that are then connected together. This function applies only to Unix domain sockets. #include <sys/socket.h> int socketpair(int family, int type, int protocol, int sockfd[2]);POSIX…