spring是如何解析xml配置文件中的占位符
作者:码农约翰的沉思录 发布时间:2023-12-02 05:57:12
前言
我们在配置Spring Xml配置文件的时候,可以在文件路径字符串中加入 ${} 占位符,Spring会自动帮我们解析占位符,这么神奇的操作Spring是怎么帮我们完成的呢?这篇文章我们就来一步步揭秘。
1.示例
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();
applicationContext.setConfigLocation("${java.version}.xml");
applicationContext.refresh();
String[] beanNames = applicationContext.getBeanDefinitionNames();
for (String beanName : beanNames) {
System.out.println(beanName);
}
这段代码在我工程里是会报错的,如下:
Caused by: java.io.FileNotFoundException: class path resource [1.8.0_144.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:190)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
... 11 more
可以看到报错里面的文件路径变成了1.8.0_144.xml,也就是说Spring帮我们把${java.version}解析成了实际值。
2.原理
AbstractRefreshableConfigApplicationContext
我们在之前的文章里提到过这个类的resolve方法,我们再来瞧一眼:
/**
* Resolve the given path, replacing placeholders with corresponding
* environment property values if necessary. Applied to config locations.
* @param path the original file path
* @return the resolved file path
* @see org.springframework.core.env.Environment#resolveRequiredPlaceholders(String)
*/
protected String resolvePath(String path) {
//通过当前环境去 解析 必要的占位符
return getEnvironment().resolveRequiredPlaceholders(path);
}
获取当前环境,这个环境在示例代码中就是 StandardEnvironment ,并且根据当前环境去解析占位符,这个占位符解析不到还会报错。
resolveRequiredPlaceHolders由StandardEnvironment的父类AbstractEnvironment实现。
AbstractEnvironment
//把propertySources放入 Resolver中
private final ConfigurablePropertyResolver propertyResolver =
new PropertySourcesPropertyResolver(this.propertySources);
@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
return this.propertyResolver.resolveRequiredPlaceholders(text);
}
这里的propertySources很重要了,从命名也可以看出我们解析占位符的来源就是从这个集合中来的。这个集合是在我们StandardEnvironment实例化的时候去自定义的。
StandardEnvironment
/**
* Create a new {@code Environment} instance, calling back to
* {@link #customizePropertySources(MutablePropertySources)} during construction to
* allow subclasses to contribute or manipulate(操作) {@link PropertySource} instances as
* appropriate.
* @see #customizePropertySources(MutablePropertySources)
*/
//StandardEnvironment 实例化调用
public AbstractEnvironment() {
customizePropertySources(this.propertySources);
}
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
//todo Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,
//todo getenv方法返回的变量大多于系统相关,
//todo getProperty方法返回的变量大多与java程序有关。
//https://www.cnblogs.com/Baronboy/p/6030443.html
propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
//SystemEnvironmentPropertySource 是System.getenv()
propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}
最重要的肯定是我们的 propertyResolver.resolveRequiredPlaceholders 方法了,propertyResolver.resolveRequiredPlaceholders其实是PropertySourcesPropertyResolver的父类AbstractPropertyResolver来实现。
AbstractPropertyResolver
//创建一个占位符的helper去解析
@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
if (this.strictHelper == null) {
//不忽略
this.strictHelper = createPlaceholderHelper(false);
}
return doResolvePlaceholders(text, this.strictHelper);
}
//私有方法
//是否忽略 无法解决的占位符
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
//默认使用${ placeholderPrefix
return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
this.valueSeparator, ignoreUnresolvablePlaceholders);
}
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
//PlaceholderResolver function interface
//todo important 重要的是这个getPropertyAsRawString
return helper.replacePlaceholders(text, this::getPropertyAsRawString);
}
这里的 this::getPropertyAsRawString 很重要,利用了java8的函数式接口来实现。它的定义在AbstractPropertyResolver里
/**
* Retrieve the specified property as a raw String,
* i.e. without resolution of nested placeholders.
* @param key the property name to resolve
* @return the property value or {@code null} if none found
*/
@Nullable
protected abstract String getPropertyAsRawString(String key);
但是我们在doResolvePlaceholders里指向的this,所以还得看PropertySourcesPropertyResolver类。
PropertySourcesPropertyResolver
//提供给函数接口 PlaceholderResolver
//todo 解析 xml配置文件路径占位符的时候调用的是这个 2020-09-11
@Override
@Nullable
protected String getPropertyAsRawString(String key) {
return getProperty(key, String.class, false);
}
@Nullable
protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
if (this.propertySources != null) {
//例如遍历的是MutablePropertySources 的propertySourceList
for (PropertySource<?> propertySource : this.propertySources) {
if (logger.isTraceEnabled()) {
logger.trace("Searching for key '" + key + "' in PropertySource '" +
propertySource.getName() + "'");
}
Object value = propertySource.getProperty(key);
if (value != null) {
//todo 解析 profile变量的时候 会去 解析 变量中的占位符 2020-09-11
//TODO 解析xml配置文件路径字符串的时候 如果占位符 变量 的值 包含占位符 在这里 不会去解析 通过Helper 去解析 PropertyPlaceholderHelper
if (resolveNestedPlaceholders && value instanceof String) {
value = resolveNestedPlaceholders((String) value);
}
logKeyFound(key, propertySource, value);
//跳出for 循环
return convertValueIfNecessary(value, targetValueType);
}
}
}
if (logger.isTraceEnabled()) {
logger.trace("Could not find key '" + key + "' in any property source");
}
return null;
}
看到没有,我们是遍历this.propertySources集合,然后根据key调用它的getProperty方法获取value。我们从上面的StandardEnvrionment中看到我们定义的是 MapPropertySource 和 SystemEnvironmentPropertySource .
MapPropertySource
//从source中取得属性
@Override
@Nullable
public Object getProperty(String name) {
return this.source.get(name);
}
这里的source就是getSystemProperties(),也就是 AbstractEnvironment中的方法:
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Map<String, Object> getSystemProperties() {
try {
//Hashtable
return (Map) System.getProperties();
}
catch (AccessControlException ex) {
return (Map) new ReadOnlySystemAttributesMap() {
@Override
@Nullable
protected String getSystemAttribute(String attributeName) {
try {
return System.getProperty(attributeName);
}
catch (AccessControlException ex) {
if (logger.isInfoEnabled()) {
logger.info("Caught AccessControlException when accessing system property '" +
attributeName + "'; its value will be returned [null]. Reason: " + ex.getMessage());
}
return null;
}
}
};
}
}
我们还忘了很重要的一步,就是PropertyPlaceholderHelper的replacePlaceholders方法。
PropertyPlaceholderHelper
//protected 范围
protected String parseStringValue(
String value, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
StringBuilder result = new StringBuilder(value);
//如果value中没有占位符前缀 那直接返回result
int startIndex = value.indexOf(this.placeholderPrefix);
while (startIndex != -1) {
//找到占位符的最后一个索引
int endIndex = findPlaceholderEndIndex(result, startIndex);
if (endIndex != -1) {
String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
String originalPlaceholder = placeholder;
if (!visitedPlaceholders.add(originalPlaceholder)) {
throw new IllegalArgumentException(
"Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
}
//1. todo 2020-09-01 解析出来占位符,比如java.version
//解析内嵌占位符
// Recursive invocation, parsing placeholders contained in the placeholder key.
placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
// Now obtain the value for the fully resolved key...
//2.todo 2020-09-01 获取实际值
String propVal = placeholderResolver.resolvePlaceholder(placeholder);
if (propVal == null && this.valueSeparator != null) {
int separatorIndex = placeholder.indexOf(this.valueSeparator);
if (separatorIndex != -1) {
String actualPlaceholder = placeholder.substring(0, separatorIndex);
String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
//这里就是实际获取占位符中值得地方。
propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
}
}
if (propVal != null) {
//从占位符里获取的值也有可能包含占位符 这里可能会报 Circular placeholder reference
propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
//替换占位符 为 实际值
result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
if (logger.isTraceEnabled()) {
logger.trace("Resolved placeholder '" + placeholder + "'");
}
startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
}
//省略部分代码
}
else {
startIndex = -1;
}
}
return result.toString();
}
到这里我们就可以看到Spring在处理一个小小的占位符就做了这么多设计。可见这个架构是如此严谨。下篇文章我们就来探讨下Spring是如何加载这个Xml文件的。
来源:https://www.tuicool.com/articles/JVBFNbe


猜你喜欢
- 淘宝物流信息TimeLine的制作方法:仿照的TimeLine效果图: 代码实现:package com.zms.timelineview;
- 通常,在这个页面中会用到很多控件,控件会用到很多的资源。Android系统本身有很多的资源,包括各种各样的字符串、图片、动画、样式和布局等等
- Android MTU 值修改的实例详解通信术语 最大传输单元(Maximum Transmission Unit,MTU)是指一种通信协议
- 问题:startTime = DateTime.Now;
- 本文实例讲述了C#实现将应用程序设置为开机启动的方法。分享给大家供大家参考。具体如下:private void WriteRegistry(
- 目录前言线程基础1、创建线程2、暂停线程3、线程等待4、线程终止C#中的lock关键字总结前言最近由于工作的需要,一直在使用C#的多线程进行
- 一.简单介绍1.配置相关的依赖2.配置模式3写.mapper、controller、service4.配置yaml文件 配置mybatis全
- 先给大家展示下效果图,如果大家感觉不错,请参考实现代码。思路1.下角Button的父View加入一个FrameLayout,也就是图中全屏透
- 由于要做一个新项目,所以打算做一个简单的图片验证码。先说说思路吧:在服务端,从一个文件夹里面找出8张图片,再把8张图片合并成一张大图,在8个
- 我们讲一下Criteria查询,这个对于不是太熟悉SQL语句的我们这些程序员来说是很容易上手的。 废话不多说,看一下例子:&nbs
- Android 使用FragmentTabhost代替Tabhost前言:现在Fragment使用越来越广了,虽然Fragment寄生在Ac
- 函数InternetGetConnectedState返回本地系统的网络连接状态。语法:BOOL InternetGetConnectedS
- Mybatis多层嵌套查询三张表:user article blog表的存储sql文件/*Navicat MySQL Data Transf
- 闹钟的简单实现,只有显示时间和设置闹钟。AlarmViewpackage com.example.lenovo.clock2; import
- Java 字节码以二进制的形式存储在 .class 文件中,每一个 .class 文件包含一个 Java 类或接口。Javaassist 就
- 本文实例为大家分享了Android扫描和生成二维码的具体代码,供大家参考,具体内容如下MainActivity.javapublic cla
- 一、存储Bean对象之前我们存储Bean时,需要在spring-config.xml中添加bean注册才行,这样的方式并不简单。我们要想更简
- 题目:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数例如在数组{7
- 一、基本回收算法 1. 引用计数(Reference Counting) 比较古老的回收算法。原理是此对象有一个引用,即增加一个计数,删除一
- 本文实例为大家分享了java实现推箱子小游戏的具体代码,供大家参考,具体内容如下二维数组二维数组:类似于二维表格(有很多层,每一层有多个房间