转载:http://my.oschina.net/ydsakyclguozi/blog/465526

1. CustomPropertyConfigurer.java

package propertyconfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.PropertyPlaceholderHelper;

public class CustomPropertyConfigurer extends PropertyPlaceholderConfigurer{
  private static Map<String,String> properties = new HashMap<String,String>();
  protected void processProperties(
    ConfigurableListableBeanFactory beanFactoryToProcess,
    Properties props) throws BeansException {
    // cache the properties
    PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
    DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
    for(Entry<Object,Object> entry:props.entrySet()){
      String stringKey = String.valueOf(entry.getKey());
      String stringValue = String.valueOf(entry.getValue());

      //用属性文件键值属性props替换字符串stringValue
      stringValue = helper.replacePlaceholders(stringValue, props);
      properties.put(stringKey, stringValue);
    }
    super.processProperties(beanFactoryToProcess, props);
  }

  public static Map<String, String> getProperties() {
    return properties;
  }

  public static String getProperty(String key){
    return properties.get(key);
  }
}
2. applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true" default-autowire="byName" default-init-method="" default-destroy-method="">

<bean id="propertyConfigurer" class="propertyconfig.CustomPropertyConfigurer">
  <property name="locations">
    <list>
      <value>classpath:propertyconfig/project.properties</value>
    </list>
  </property>
</bean>
</beans>

3. project.properties

site=iteye
blog=antlove
url=${site}/${blog}
4. Main.java测试类

package propertyconfig;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
  public static void main(String[] args) {
    new ClassPathXmlApplicationContext("propertyconfig/applicationContext.xml");

    Map<String,String> properties = CustomPropertyConfigurer.getProperties();

    System.out.println(properties);
  }
}

Spring源码学习-PropertyPlaceholderHelper的更多相关文章

  1. spring源码学习之路---深入AOP(终)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...

  2. spring源码学习之路---IOC初探(二)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...

  3. Spring源码学习

    Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...

  4. Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md

    写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...

  5. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签

    写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...

  6. Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作

    写在前面 上文 Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件主要讲Spring容器创建时通过XmlBeanDefinitionReader读 ...

  7. Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件

    写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...

  8. 【目录】Spring 源码学习

    [目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...

  9. Spring 源码学习——Aop

    Spring 源码学习--Aop 什么是 AOP 以下是百度百科的解释:AOP 为 Aspect Oriented Programming 的缩写,意为:面向切面编程通过预编译的方式和运行期动态代理实 ...

随机推荐

  1. FZU 2214 Knapsack problem(背包问题)

    Description 题目描述 Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...

  2. So easy Webservice 4.Java方式访问WebService(使用jdk1.6以上 wsimport命令)

    1.选中要调用的服务单击”服务说明” 2.获取wsdl文件.使用JDK1.6以上的版本的wsimport命令 a) 例如选中:http://webservice.webxml.com.cn/WebSe ...

  3. python 自动生成C++代码 (代码生成器)

    python 代码自动生成的方法 (代码生成器) 遇到的问题 工作中遇到这么一个事,需要写很多C++的底层数据库类,但这些类大同小异,无非是增删改查,如果人工来写代码,既费力又容易出错:而借用pyth ...

  4. SublimeText2 快捷键

    前端神器Sublime Text3使用说明:http://www.jianshu.com/p/1f2463261ded SublimeText2 快捷键,与对应功能一览表: 快捷键 功能 ctrl+s ...

  5. 一个js(javascript)使用案例

    <script type="text/javascript"> var Row; $(function () { // $("#Sel").clic ...

  6. Ant build ${renderscript.opt.level}问题解决方案

    问题如下: BUILD FAILEDD:\adt-bundle-windows-x86_64-20131030\sdk\tools\ant\build.xml:653: The following e ...

  7. Gradient Boosted Regression Trees 2

    Gradient Boosted Regression Trees 2   Regularization GBRT provide three knobs to control overfitting ...

  8. NSDate使用

    //获取当前日期,时间 +(NSDate *)getCurrentDate{     NSDate *now = [NSDate date];     return now; } //将日期转换为字符 ...

  9. [玲珑OJ1044] Quailty and Binary Operation (FFT+cdq分治)

    题目链接 题意:给定两个长度为n的数组a与长度为m的数组b, 给定一个操作符op满足 x op y = x < y ? x+y : x-y.  有q个询问,每次给出询问c,问:有多少对(i, j ...

  10. 百度web前端面试2015.10.18

    邮件里通知的周日下午两点参加百度校招面试,我13:10分就到了,前台先让我拿了个面试资格单(上面是我的信息),然后在web前端面试入口排队,面试在百度食堂举行的,等了大概1个小时,放我去面试.都是一对 ...