Spring PropertyPlaceholderConfigurer 自定义扩展

参考:http://seraph115.iteye.com/blog/435165

参考:https://blog.csdn.net/feiyu8607/article/details/8282893

Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spring配置期间替换使用属性值。接下来让我们逐渐的深入其配置。

 基本的使用方法是:

[html] view plain copy
  1. <bean id="propertyConfigurerForAnalysis"   
  2.       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  3.     <property name="location">    
  4.         <value>classpath:/spring/include/dbQuery.properties</value>    
  5.     </property>    
  6. </bean>   

其中classpath是引用src目录下的文件写法。

当存在多个Properties文件时,配置就需使用locations了:(2)

[html] view plain copy
  1. <bean id="propertyConfigurer"   
  2.       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  3.     <property name="locations">    
  4.        <list>    
  5.           <value>classpath:/spring/include/jdbc-parms.properties</value>    
  6.           <value>classpath:/spring/include/base-config.properties</value>    
  7.         </list>    
  8.     </property>    
  9. </bean>     

接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties文件,其配置如下:(3)
  

[html] view plain copy
  1. <bean id="propertyConfigurerForProject1"   
  2.       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  3.     <property name="order" value="1" />    
  4.     <property name="ignoreUnresolvablePlaceholders" value="true" />   
  5.     <property name="locations">    
  6.       <list>     
  7.         <value>classpath:/spring/include/dbQuery.properties</value>       
  8.       </list>  
  9.     </property>     
  10. </bean>    
  11.    
  12. <bean id="propertyConfigurerForProject2"   
  13.       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     
  14.     <property name="order" value="2" />     
  15.     <property name="ignoreUnresolvablePlaceholders" value="true" />     
  16.     <property name="locations">     
  17.       <list>     
  18.         <value>classpath:/spring/include/jdbc-parms.properties</value>     
  19.         <value>classpath:/spring/include/base-config.properties</value>     
  20.       </list>     
  21.     </property>     
  22. </bean>  

其中order属性代表其加载的顺序,如果没有设置就按照加载xml文件时的顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如果配置了多个PropertyPlaceholderConfigurer,则该属性必须设置且为true,否则propertyConfigurerForProject2的properties文件不会被加载.

至此你已经了解到了如何使用PropertyPlaceholderConfigurer,如何使用多个Properties文件,以及如何配置多个PropertyPlaceholderConfigurer来分解工程中分散的Properties文件。至于PropertyPlaceholderConfigurer还有更多的扩展应用,如属性文件加密解密等方法将在之后的博文中续写。

注意事项:
(1)如果上面的dbQuery.properties与jdbc-parms.properties文件中有相同的参数配置名称,dbQuery.properties中配置的参数值不会被后面的覆盖;
(2)如果jdbc-parms.properties,base-config.properties彼此有相同参数名配置,jdbc-parms.properties中的配置的值会被覆盖;
 

自定义扩展PropertyPlaceholderConfigurer实现

例如:配置文件的路径,需要动态确定的,就需要自己扩展PropertyPlaceholderConfigurer的实现,自己获取文件路径,load properties文件,然后将load后的properties加入PropertyPlaceholderConfigurer

[java] view plain copy
  1. package com.common.spring.ext;  
  2.   
  3. import java.util.Properties;  
  4. import java.util.Set;  
  5.   
  6. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;  
  7.   
  8. import com.common.exception.ApplicationException;  
  9. import com.common.util.GlobalProperties;  
  10. import com.common.util.PropertiesUtil;  
  11.   
  12.   
  13. public class GollfPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {  
  14.   
  15.     public void setGollfPropFiles(Set<String> gollfPropFiles) {  
  16.         String propPath = GlobalProperties.getProperty(GlobalProperties.PROPERTIES_FOLDER_PATH); //通过其他配置获取路径  
  17.         String fileSeparator = System.getProperty("file.separator");  
  18.   
  19.         Properties properties = new Properties();  
  20.         for (String gollfPropFile : gollfPropFiles) {  
  21.   
  22.             String nodeName = System.getProperty("weblogic.Name");  
  23.             gollfPropFile = gollfPropFile.replaceAll("\\[NODE_NAME\\]", nodeName); //NODE_NAME 是根据不同weblogic server确定  
  24.   
  25.             String file = propPath + fileSeparator + gollfPropFile;  
  26.   
  27.             try {  
  28.                 logger.info("Loading properites file from " + file);  
  29.                 Properties prop = PropertiesUtil.loadProperties(file); //返回properties文件  
  30.                 logger.debug("Properties -> " + prop);  
  31.                 if(prop != null) {  
  32.                     properties.putAll(prop);  
  33.                 }  
  34.             } catch (Exception e) {  
  35.                 logger.fatal(new ApplicationException("Properties file " + gollfPropFile +   
  36.             " cannot be found. All related functionalities may be unavailable", e, true));  
  37.             }  
  38.         }  
  39.   
  40.         this.setProperties(properties); //关键方法,调用的PropertyPlaceholderConfigurer中的方法,  
  41.                    //通过这个方法将自定义加载的properties文件加入spring中  
  42.     }  
  43.   
  44. }  

xml配置

[html] view plain copy
  1. <bean id="auditJmsProperties"   
  2.       class="com.common.spring.ext.GollfPropertyPlaceholderConfigurer">  
  3.        <property name="gollfPropFiles">  
  4.            <set>  
  5.                <value>[NODE_NAME]_jms.properties</value>  
  6.            </set>  
  7.        </property>  
  8.    </bean>  


PropertyPlaceholderConfigurer中加载properties文件时,实际调用的:org.springframework.core.io.support.PropertiesLoaderSupport中的mergeProperties

Spring源码

[java] view plain copy
  1. protected Properties mergeProperties() throws IOException {  
  2.   Properties result = new Properties();  
  3.   
  4.   if (this.localOverride) {  
  5.    // Load properties from file upfront, to let local properties override.  
  6.    loadProperties(result);  
  7.   }  
  8.   
  9.   if (this.localProperties != null) {  
  10.    for (Properties localProp : this.localProperties) {  
  11.        //将用户自定义加载的属性值,与spring加载的合并  
  12.        CollectionUtils.mergePropertiesIntoMap(localProp, result);   
  13.      }  
  14.   }  
  15.   
  16.   if (!this.localOverride) {  
  17.    // Load properties from file afterwards, to let those properties override.  
  18.    loadProperties(result);  
  19.   }  
  20.   
  21.   return result;  
  22.  }  

 
 
将多个properties文件中的配置加载以后合并成一个Properties对象返回.
上面的this.setProperties(properties)方法,就是设置localProperties的引用,localProperties不为空的话,将用户自定义加载的properties属性合并到Spring加载的result Properties对象中
localOverride参数:为true的话,表示用户自定义加载的属性值覆盖spring系统加载的,如果同名的话.

自定义使用注意:用户自定义方法的调用务必在spring 初始化调用PropertyPlaceholderConfigurer的mergeProperties()方法之前调用,否则配置文件就没有合并.一般就set值的时候调用.


猜你喜欢

转载自blog.csdn.net/menglonghuanying/article/details/80087058