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

2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:

<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

   <property name="location">

     <value>conf/sqlmap/jdbc.properties</value>

   </property>

    <property name="fileEncoding">

      <value>UTF-8</value>

    </property>

</bean>

当然也可以引入多个属性文件,如:

<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="locations">

   <list>

    <value>/WEB-INF/mail.properties</value>  

    <value>classpath: conf/sqlmap/jdbc.properties</value>//注意这两种value值的写法

   </list>

  </property>

</bean>

3.譬如,jdbc.properties的内容为:

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=round;

jdbc.username=root

jdbc.password=123456

4.那么在spring配置文件中,我们就可以这样写:

<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="locations">

   <list>

    <value>classpath: conf/sqlmap/jdbc.properties </value>

   </list>

  </property>

</bean>

<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

  <property name="driverClassName"value="${jdbc.driverClassName}" />

  <property name="url" value="${jdbc.url}" />

  <property name="username" value="${jdbc.username}"/>

  <property name="password"value="${jdbc.password}" />

</bean>

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

6.查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。

PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。

我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。

Spring--PropertyPlaceholderConfigurer的更多相关文章

  1. spring PropertyPlaceholderConfigurer 找不到配置文件原因

    1:  spring 版本问题 参见: http://www.cnblogs.com/alex-blog/archive/2012/12/25/2832357.html 2: bean id 同名   ...

  2. Spring PropertyPlaceholderConfigurer占位符用法

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

  3. 使用Spring PropertyPlaceholderConfigurer 配置中文出现乱码的解决方法

    在使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 读取配置文件时,发现对于中文的处理会出现乱码现象,比 ...

  4. Spring PropertyPlaceholderConfigurer类载入外部配置

    2019独角兽企业重金招聘Python工程师标准>>> 通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.co ...

  5. Spring PropertyPlaceholderConfigurer 自定义扩展

    原文地址:https://blog.csdn.net/feiyu8607/article/details/8282893 Spring中PropertyPlaceholderConfigurer这个类 ...

  6. Spring PropertyPlaceholderConfigurer数据库配置

    pom.xml中添加依赖 <!-- mysql-connector-java --> <dependency> <groupId>mysql</groupId ...

  7. spring读取数据库的配置信息(url、username、password)时的<bean>PropertyPlaceholderConfigurer的用法

    用法1: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...

  8. Spring 利用PropertyPlaceholderConfigurer占位符

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

  9. Spring基础知识

    Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...

  10. 【Spring实战】----开篇(包含系列目录链接)

    [Spring实战]----开篇(包含系列目录链接) 置顶2016年11月10日 11:12:56 阅读数:3617 终于还是要对Spring进行解剖,接下来Spring实战篇系列会以应用了Sprin ...

随机推荐

  1. 学堂在线 UWP 首版

    好久没有写博客了,主要是最近在写一个小小的App.<( ̄︶ ̄)> 不知道看各位有木有爱看慕课的,作为一名资深的大三学渣的我有看慕课的习惯.一直在看学堂在线的慕课,感觉质量确实还可以,但是遗 ...

  2. 代码生成工具——CodeSmith

    1.绿色版软件下载:http://download.csdn.net/detail/laoge/6859701 2.破解说明:http://tieba.baidu.com/p/3373160396 3 ...

  3. Tomcat本地服务器搭建

    首先,下载jdk-8u111-windows-x64.exe,然后配置环境,以安装目录D:\jdk1.8.0_111为例: 新建一个变量: 然后打开path新建两个变量: 最后去控制台敲javac或者 ...

  4. 搭建企业内部yum仓库(centos6+centos7+epel源)

    搭建自己的yum仓库,将自己制作好的rpm包,添加到自己的yum源中. yum仓库服务端配置如下 : 1. 创建yum仓库目录 mkdir -p /data/yum_data/cd /data/yum ...

  5. C#利用iComparable接口实现List排序

    List<T>类可以使用Sort()方法对元素排序. Sort()方法定义了几个重载方法,分别是  public void List<T>.Sort(),不带有任何参数的Sor ...

  6. re模块(正则表达式)

    re 模块:正则表达式import re 内置模块: 1> re.match(pattern,string) pattern:就是正则表达式 string:被操作的对象 match,search ...

  7. FTP 的搭建过程和遇到的问题

    http://linux.it.net.cn/e/server/ftp/2015/0227/13554.htmlhttps://mp.weixin.qq.com/s?__biz=MzA3OTgyMDc ...

  8. javascript原型Prototype【转】

    转自:http://www.cnblogs.com/starof/p/4190404.html 在javaScript创建对象一文中提到过:用构造函数创建对象存在一个问题即同一构造函数的不同实例的相同 ...

  9. Python列表、元组、字典和字符串的常用函数

    Python列表.元组.字典和字符串的常用函数 一.列表方法 1.ls.extend(object) 向列表ls中插入object中的每个元素,object可以是字符串,元组和列表(字符串“abc”中 ...

  10. Struts中Action三种接收参数的方式?

    前言: 前面已经有一篇随笔介绍了Struts2的大概原理.本文就Struts2中Action与jsp页面进行数据对接时介绍几种常见方法! 值栈ValueStack 3个Action Action1 p ...