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); @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,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 佔位符的資料請關注我們其它相關文章!