转载: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. linux的计划任务crontab

    crontab(全称cron table计划任务列表)是一个用于周期性被执行的任的工具. 相关指令: usage: crontab [-u user] file   crontab [ -u user ...

  2. hdu 5150 Sum Sum Sum 水

    Sum Sum Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  3. hdu 5587 规律

    题意:开始序列{1}; 一次变换{1,1,2}: 两次变换{1,1,2,1,2,2,3} ... 求s[n];题解:打表 S1,S2,S4,S8,S16,S32......公式 S[n]=S[最近的比 ...

  4. 你不知道的css中的expression

    expression是在IE5版本之后支持使用的,用来把CSS属性和Javascript脚本关联起来,[这里的CSS属性可以是元素固有的属性,也可以是自定义属性.]是动态设置CSS属性的强大方法,但也 ...

  5. 细说 Request[]与Request.Params[]

    http://www.cnblogs.com/fish-li/archive/2011/12/06/2278463.html

  6. FTP常用故障代码注解

    FTP错误列表 出处:http://bbs.enet.com.cn/UserControl?act=13&threadID 作者: |秒杀』| 详细的FTP错误列表 Restart marke ...

  7. MySql 去重且指定某字段在前的排序方法

    今天遇到一个棘手的数据查找并去重的问题: 情况: 1.取出数据库中的数据: 2.同一字段A,不同情况<值,如A值为:a1,a2>下取出的其他数据可能相同: 3.将2情况下的重复数据< ...

  8. meta标签清理缓存

    如果需要在html页面上设置不缓存,这在<head>标签中加入如下语句: <meta http-equiv="Pragma" content="no-c ...

  9. 搭建Mono for Android开发环境(用离线版)

    上面为我现在的离线版的安装包内容(不知道为什么上传的图名字显示不出来,郁闷),这些文件可以到http://pan.baidu.com/s/1ntM8U4T这里去下载: 安装步骤如下: 1)jdk-6u ...

  10. mysql help

    1.一般情况,不知道命令的使用方法,有三种办法: a. --help 是命令的一个选项,介绍命令的使用方法.mysql --help 或者mysql -? b. man 对命令的详细解释,man my ...