spring是如何解析xml配置文件中的占位符
前言
我們在配置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) {
//通過當(dāng)前環(huán)境去 解析 必要的占位符
return getEnvironment().resolveRequiredPlaceholders(path);
}
獲取當(dāng)前環(huán)境,這個環(huán)境在示例代碼中就是 StandardEnvironment ,并且根據(jù)當(dāng)前環(huán)境去解析占位符,這個占位符解析不到還會報錯。
resolveRequiredPlaceHolders由StandardEnvironment的父類AbstractEnvironment實現(xiàn)。
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 實例化調(diào)用
public AbstractEnvironment() {
customizePropertySources(this.propertySources);
}
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
//todo Java提供了System類的靜態(tài)方法getenv()和getProperty()用于返回系統(tǒng)相關(guān)的變量與屬性,
//todo getenv方法返回的變量大多于系統(tǒng)相關(guān),
//todo getProperty方法返回的變量大多與java程序有關(guān)。
//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來實現(xiàn)。
AbstractPropertyResolver
//創(chuàng)建一個占位符的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) {
//默認(rèn)使用${ 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的函數(shù)式接口來實現(xiàn)。它的定義在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
//提供給函數(shù)接口 PlaceholderResolver
//todo 解析 xml配置文件路徑占位符的時候調(diào)用的是這個 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 循環(huán)
return convertValueIfNecessary(value, targetValueType);
}
}
}
if (logger.isTraceEnabled()) {
logger.trace("Could not find key '" + key + "' in any property source");
}
return null;
}
看到?jīng)]有,我們是遍歷this.propertySources集合,然后根據(jù)key調(diào)用它的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
//解析內(nèi)嵌占位符
// 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在處理一個小小的占位符就做了這么多設(shè)計??梢娺@個架構(gòu)是如此嚴(yán)謹(jǐn)。下篇文章我們就來探討下Spring是如何加載這個Xml文件的。
以上就是spring是如何解析xml配置文件中的占位符的詳細(xì)內(nèi)容,更多關(guān)于spring解析xml 占位符的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java?ThreadPoolExecutor線程池有關(guān)介紹
這篇文章主要介紹了Java?ThreadPoolExecutor線程池有關(guān)介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
Java基礎(chǔ)入門篇之邏輯控制練習(xí)題與猜數(shù)字游戲
猜數(shù)字游戲是一款經(jīng)典的游戲,該游戲說簡單也很簡單,說不簡單確實也很難,這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)入門篇之邏輯控制練習(xí)題與猜數(shù)字游戲的相關(guān)資料,需要的朋友可以參考下2023-06-06
SpringCloud使用Feign實現(xiàn)遠(yuǎn)程調(diào)用流程詳細(xì)介紹
OpenFeign源于Netflix的Feign,是http通信的客戶端。屏蔽了網(wǎng)絡(luò)通信的細(xì)節(jié),直接面向接口的方式開發(fā),讓開發(fā)者感知不到網(wǎng)絡(luò)通信細(xì)節(jié)。所有遠(yuǎn)程調(diào)用,都像調(diào)用本地方法一樣完成2023-02-02
在Ubuntu系統(tǒng)下安裝JDK和Tomcat的教程
這篇文章主要介紹了在Ubuntu系統(tǒng)下安裝JDK和Tomcat的教程,這樣便是在Linux系統(tǒng)下搭建完整的Java和JSP開發(fā)環(huán)境,需要的朋友可以參考下2015-08-08
關(guān)于Spring MVC框架中攔截器Interceptor的使用解讀
這篇文章主要介紹了關(guān)于Spring MVC框架中攔截器Interceptor的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

