转载: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. git学习笔记06-创建分支合并分支-比svn快多了,因为只有指针在改变

    一开始git只有一条时间线,这个分支叫主分支,即master分支. HEAD严格来说不是指向提交,而是指向master,master才是指向提交的,所以,HEAD指向的就是当前分支. 每次提交,mas ...

  2. JavaScript删除-confirm

    一> onclick="javascript:if (confirm('您确定要删除吗?注意:此操作不可恢复,请谨慎操作!')){return true;} return false; ...

  3. 关于css3的自定义字体

    css3的@font-face属性打破了中文页面字体一成不变的格局,但今天本菜在用的时候并不那么爽.开始各种引用外部ttf文件失败.下了300M+的字体文件,苦逼的试了一下午.终于有一个ttf引用成功 ...

  4. poj3714Raid(平面最近点对)

    链接 模板 稍加一点标记 模板 #include <iostream> #include<cstdio> #include<cstring> #include< ...

  5. asp.net开发中经常用到的方法

    ---天气插件--- <iframe width="560" scrolling="no" height="23" framebord ...

  6. windows volume related concepts

    Volume --- 一个volume就是一个分区.  在该“物理硬盘”上创建一个或多个分区,再创建文件系统,才可以得到一个VOLUME.此时VOLUME相对于主机是一个逻辑设备. https://m ...

  7. heaters

    https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...

  8. ios7适配一些问题以及64位32位

    ios7适配一些问题(http://www.cocoachina.com/ios/20130703/6526.html) 1.iOS应用如何实现64位的支持 http://www.codeceo.co ...

  9. Node.js superagent 采集 URL 编码问题

    今天在用Node学习采集的时候遇到一个问题,如这个链接地址 http://www.meishij.net/胡萝卜  就是用浏览器的方式访问链接可以打开,但用superagent 去模拟请求,就请求不到 ...

  10. $.extend abc

    console.log(jQuery.extend(this,{'a':'b'},{'c':'d'}));console.log(this.a)var tt = jQuery.extend({},{' ...