spring是如何解析xml配置文件中的占位符
前言
我們?cè)谂渲肧pring Xml配置文件的時(shí)候,可以在文件路徑字符串中加入 ${} 占位符,Spring會(huì)自動(dòng)幫我們解析占位符,這么神奇的操作Spring是怎么幫我們完成的呢?這篇文章我們就來(lái)一步步揭秘。
1.示例
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();applicationContext.setConfigLocation('${java.version}.xml');applicationContext.refresh();String[] beanNames = applicationContext.getBeanDefinitionNames();for (String beanName : beanNames) { System.out.println(beanName);}
這段代碼在我工程里是會(huì)報(bào)錯(cuò)的,如下:
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
可以看到報(bào)錯(cuò)里面的文件路徑變成了1.8.0_144.xml,也就是說(shuō)Spring幫我們把${java.version}解析成了實(shí)際值。
2.原理
AbstractRefreshableConfigApplicationContext我們?cè)谥暗奈恼吕锾岬竭^(guò)這個(gè)類的resolve方法,我們?cè)賮?lái)瞧一眼:
/** * 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) { //通過(guò)當(dāng)前環(huán)境去 解析 必要的占位符 return getEnvironment().resolveRequiredPlaceholders(path); }
獲取當(dāng)前環(huán)境,這個(gè)環(huán)境在示例代碼中就是 StandardEnvironment ,并且根據(jù)當(dāng)前環(huán)境去解析占位符,這個(gè)占位符解析不到還會(huì)報(bào)錯(cuò)。
resolveRequiredPlaceHolders由StandardEnvironment的父類AbstractEnvironment實(shí)現(xiàn)。
AbstractEnvironment
//把propertySources放入 Resolver中private final ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);@Overridepublic String resolveRequiredPlaceholders(String text) throws IllegalArgumentException { return this.propertyResolver.resolveRequiredPlaceholders(text);}
這里的propertySources很重要了,從命名也可以看出我們解析占位符的來(lái)源就是從這個(gè)集合中來(lái)的。這個(gè)集合是在我們StandardEnvironment實(shí)例化的時(shí)候去自定義的。
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 實(shí)例化調(diào)用 public AbstractEnvironment() { customizePropertySources(this.propertySources); }@Overrideprotected 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其實(shí)是PropertySourcesPropertyResolver的父類AbstractPropertyResolver來(lái)實(shí)現(xiàn)。
AbstractPropertyResolver
//創(chuàng)建一個(gè)占位符的helper去解析@Overridepublic String resolveRequiredPlaceholders(String text) throws IllegalArgumentException { if (this.strictHelper == null) { //不忽略 this.strictHelper = createPlaceholderHelper(false); } return doResolvePlaceholders(text, this.strictHelper);} //私有方法 //是否忽略 無(wú)法解決的占位符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 重要的是這個(gè)getPropertyAsRawString return helper.replacePlaceholders(text, this::getPropertyAsRawString); }
這里的 this::getPropertyAsRawString 很重要,利用了java8的函數(shù)式接口來(lái)實(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);
但是我們?cè)赿oResolvePlaceholders里指向的this,所以還得看PropertySourcesPropertyResolver類。
PropertySourcesPropertyResolver
//提供給函數(shù)接口 PlaceholderResolver //todo 解析 xml配置文件路徑占位符的時(shí)候調(diào)用的是這個(gè) 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變量的時(shí)候 會(huì)去 解析 變量中的占位符 2020-09-11 //TODO 解析xml配置文件路徑字符串的時(shí)候 如果占位符 變量 的值 包含占位符 在這里 不會(huì)去解析 通過(guò)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@Nullablepublic 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中沒(méi)有占位符前綴 那直接返回result int startIndex = value.indexOf(this.placeholderPrefix); while (startIndex != -1) { //找到占位符的最后一個(gè)索引 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 解析出來(lái)占位符,比如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 獲取實(shí)際值 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()); //這里就是實(shí)際獲取占位符中值得地方。 propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder); } } if (propVal != null) { //從占位符里獲取的值也有可能包含占位符 這里可能會(huì)報(bào) Circular placeholder reference propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders); //替換占位符 為 實(shí)際值 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在處理一個(gè)小小的占位符就做了這么多設(shè)計(jì)。可見(jiàn)這個(gè)架構(gòu)是如此嚴(yán)謹(jǐn)。下篇文章我們就來(lái)探討下Spring是如何加載這個(gè)Xml文件的。
以上就是spring是如何解析xml配置文件中的占位符的詳細(xì)內(nèi)容,更多關(guān)于spring解析xml 占位符的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案3. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單4. 使用純HTML的通用數(shù)據(jù)管理和服務(wù)5. CSS hack用法案例詳解6. css代碼優(yōu)化的12個(gè)技巧7. css進(jìn)階學(xué)習(xí) 選擇符8. HTML DOM setInterval和clearInterval方法案例詳解9. 使用css實(shí)現(xiàn)全兼容tooltip提示框10. Vue+elementUI下拉框自定義顏色選擇器方式
