Spring自定义PropertyPlaceholderConfigurer

1. CustomPropertyConfigurer.Java

Java代码   收藏代码
  1. package propertyconfig;  
  2. import java.util.HashMap;  
  3. import java.util.Map;  
  4. import java.util.Properties;  
  5. import java.util.Map.Entry;  
  6. import org.springframework.beans.BeansException;  
  7. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  8. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;  
  9. import org.springframework.util.PropertyPlaceholderHelper;  
  10.   
  11. public class CustomPropertyConfigurer extends PropertyPlaceholderConfigurer{  
  12.     private static Map<String,String> properties = new HashMap<String,String>();  
  13.     protected void processProperties(  
  14.             ConfigurableListableBeanFactory beanFactoryToProcess,  
  15.             Properties props) throws BeansException {  
  16.         // cache the properties  
  17.         PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(  
  18.                 DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);  
  19.         for(Entry<Object,Object> entry:props.entrySet()){  
  20.             String stringKey = String.valueOf(entry.getKey());  
  21.             String stringValue = String.valueOf(entry.getValue());  
  22.             stringValue = helper.replacePlaceholders(stringValue, props);  
  23.             properties.put(stringKey, stringValue);  
  24.         }  
  25.         super.processProperties(beanFactoryToProcess, props);  
  26.     }  
  27.       
  28.     public static Map<String, String> getProperties() {  
  29.         return properties;  
  30.     }  
  31.       
  32.     public static String getProperty(String key){  
  33.         return properties.get(key);  
  34.     }  
  35. }  

 2. applicationContext.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    
  4.     xmlns:aop="http://www.springframework.org/schema/aop"    
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  6.     http://www.springframework.org/schema/aop    
  7.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"    
  8.     default-lazy-init="true" default-autowire="byName" default-init-method="" default-destroy-method="">    
  9.     <bean id="propertyConfigurer"  class="propertyconfig.CustomPropertyConfigurer">    
  10.         <property name="locations">    
  11.             <list>    
  12.                 <value>classpath:propertyconfig/project.properties</value>    
  13.             </list>    
  14.         </property>    
  15.     </bean>    
  16. </beans>    

 3. project.properties

Properties代码   收藏代码
  1. site=iteye  
  2. blog=antlove  
  3. url=${site}/${blog}  

 4. Main.java测试

Java代码   收藏代码
  1. package propertyconfig;  
  2. import java.util.Map;  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. public class Main {  
  5.     public static void main(String[] args) {  
  6.         new ClassPathXmlApplicationContext("propertyconfig/applicationContext.xml");  
  7.           
  8.         Map<String,String> properties = CustomPropertyConfigurer.getProperties();  
  9.           
  10.         System.out.println(properties);  
  11.     }  
  12. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值