在开发的过程中,经常发现一些类似:${log4j.level}之类的内容,后来才知道原因。下面解释一下:

1、PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。

2、在Spring中,使用PropertyPlaceholderConfigurer可以在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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.***.data.***" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:project.properties</value>
</property>
</bean>
<import resource="classpath*:/db/*.xml" />
<import resource="classpath*:/common/*.xml" />
</beans>

2、文件antx.properties:

session.mode=test
session.userId=99758820
session.testDate=2013-07-17

3、在其他的xml文件里可以这么写:

        <bean id="sessionConfig" class="com.***.data.***.acl.SessionConfig">
<property name="mode">
<value>${session.mode}</value>
</property>
<property name="userId">
<value>${session.userId}</value>
</property>
<property name="testDate">
<value>${session.testDate}</value>
</property>
</bean>

4、这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。

5、当然也可以在JAVA代码中使用

private static final Properties sysConfig = new Properties();

    static {
try {
InputStream iStream = new FileInputStream(new File("config", "antx.properties"));
sysConfig.load(iStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static String getPropertyValue(String key){
return sysConfig.getProperty(key);
}

Spring的PropertyPlaceholderConfigurer事例应用的更多相关文章

  1. Spring中PropertyPlaceholderConfigurer的使用

    Spring中PropertyPlaceholderConfigurer的使用     在使用Spring配置获取properties文件时,在网上查到相关的资料,分享哈!! (1)获取一个配置文件 ...

  2. Spring的PropertyPlaceholderConfigurer应用

    Spring 利用PropertyPlaceholderConfigurer占位符 1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFa ...

  3. Spring 利用PropertyPlaceholderConfigurer占位符

      Hey Girl   博客园    首页    博问    闪存    新随笔    订阅     管理 posts - 42,  comments - 3,  trackbacks - 0 Sp ...

  4. Spring的PropertyPlaceholderConfigurer应用(转)

    转自:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html Spring 利用PropertyPlaceholderConfigu ...

  5. Spring里PropertyPlaceholderConfigurer类的使用

    1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...

  6. spring中propertyplaceholderconfigurer简介

    Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyP ...

  7. spring 的 PropertyPlaceholderConfigurer读取的属性怎么访问 (java访问方式,不是xml中的占位符哦)及此类的应用

    一.1.占位符的应用:(@Autowired注解方式,不需要建立set与get方法了,xml注入也不需要写了) http://www.cnblogs.com/susuyu/archive/2012/0 ...

  8. Spring的PropertyPlaceholderConfigurer强制使用默认值的坑

    1.问题 dubbo client配置: <dubbo:reference id="channelCustomerClient" interface="com.gt ...

  9. Spring的PropertyPlaceholderConfigurer

    在项目中我们一般将配置信息(如数据库的配置信息)配置在一个properties文件中,如下: jdbcUrl=jdbc:mysql://localhost:3306/flowable?useUnico ...

随机推荐

  1. ArcGIS 10.2 链接64位Oracle数据库

    https://my.oschina.net/zctzl/blog/909541 来自:https://blog.csdn.net/LoveCarpenter/article/details/5954 ...

  2. String转Date的类型转换器

    import org.apache.commons.beanutils.Converter; import org.apache.commons.lang.StringUtils; /* * 定义转换 ...

  3. Glibc 和 uClibc

    转自:https://blog.csdn.net/clirus/article/details/50145959?locationNum=4 最近在搞mips openwrt框架的东西,mipc的GC ...

  4. java7 NIO2 watching service API

    java7 NIO2新增了文件系统的相关事件处理API,为目录,文件新增修改删除等事件添加事件处理. package reyo.sdk.utils.file; import java.io.IOExc ...

  5. 删除数据库字段一样的row, 并增加唯一索引

    DELETE FROM `groups` WHERE name IN (select a.name from ( SELECT name FROM `groups` GROUP BY name HAV ...

  6. Node.js SDK与fabric链码交互开发

    1.本篇背景 前面已经对链码开发作了比较详细的介绍,并且对官方提供的 fabcar 链码进行了解读,本篇将介绍如何使用 Node.js SDK 与区块链网络中的链码进行交互. 本篇内容基本来自官方 H ...

  7. 代理服务器polipo;socks5代理转http代理

    安装: brew install polipo 使用: To have launchd start polipo now and restart at login: brew services sta ...

  8. Java 内存管理白皮书

    1. 垃圾回收器 职责 分配内存 保证有引用对象不被回收 保证无引用对象被回收 设计方式 串行(Serial)与并行(Parallel) 串行的回收方式, 每次只能执行一种操作. 例如, 在多 cpu ...

  9. 请简单介绍一下什么是Spring?

    Spring的核心是一个轻量级(Lightweight)的容器(Container),它是实现IoC(Inversion of Control)容器和非入侵性(No intrusive)的框架,并提供 ...

  10. scala编程第16章学习笔记(2)

    转换列表:toIterator, toArray,copyToArray List类的toArray方法将递归存放的列表转换为连续存放的数组 Array类的toList方法将连续存放的数组转换为递归存 ...