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) { //通過當前環境去 解析 必要的占位符 return getEnvironment().resolveRequiredPlaceholders(path); }
獲取當前環境,這個環境在示例代碼中就是 StandardEnvironment ,并且根據當前環境去解析占位符,這個占位符解析不到還會報錯。
resolveRequiredPlaceHolders由StandardEnvironment的父類AbstractEnvironment實現。
AbstractEnvironment
//把propertySources放入 Resolver中private final ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);@Overridepublic 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); }@Overrideprotected 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去解析@Overridepublic 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@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中沒有占位符前綴 那直接返回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文件的。
以上就是spring是如何解析xml配置文件中的占位符的詳細內容,更多關于spring解析xml 占位符的資料請關注好吧啦網其它相關文章!
相關文章: