Spring 利用PropertyPlaceholderConfigurer占位符

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的使用

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

  2. Spring 利用PropertyPlaceholderConfigurer占位符

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

  3. Spring的PropertyPlaceholderConfigurer应用(转)

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

  4. Spring里PropertyPlaceholderConfigurer类的使用

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

  5. spring中propertyplaceholderconfigurer简介

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

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

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

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

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

  8. Spring的PropertyPlaceholderConfigurer

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

  9. Spring的PropertyPlaceholderConfigurer事例应用

    在开发的过程中,经常发现一些类似:${log4j.level}之类的内容,后来才知道原因.下面解释一下: 1.PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现 ...

随机推荐

  1. js中几个容易混淆的概念

    1. var name = "The Window";var object = {name : "My Object",getName: function(){ ...

  2. 简单的thinkPHP3.2运行实例。

    在上一篇的环境基础下. 我们用zendstudio12.5版本编写我们的代码.具体的下载方式在这里就不多做注明了.自己百度就可以搞定. 首先我们用zendstudio12.5 导入我们从网上随处都可以 ...

  3. 最新版AltiumDesignerSummer9下载+破解

    下载地址:ed2k://|file|AltiumDesignerSummer9Build9.3.1.19182.7z|1875307483|e65d364bf987fb5dcfb81c081a1562 ...

  4. Seafile的手册

    http://manual.seafile.com/http://manual-cn.seafile.com/deploy/using_mysql.html 中文版http://manual-cn.s ...

  5. SxsTrace工具使用方法

    Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库的版本和路径.   SxsTrace使用的方法:   1.首先必须以Administrator用户身份登录 ...

  6. HDU1394 Minimum Inversion Number(线段树OR归并排序)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  7. July 【补题】

    A(zoj 3596) bfs,记忆搜都可以, 按余数来记录状态. B(zoj 3599) 博弈,跳过 C(zoj 3592) 简单dp,题意不好懂 D(zoj 3602) 子树哈希, 对根的左右儿子 ...

  8. hdu 5288 OO’s Sequence(计数)

    Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...

  9. web前端之 HTML标签详细介绍

    html标签的分类 点我查看完整的html标签介绍 在html中,标签一般分为块级标签和行内标签 块级标签:块元素一般都从新行开始,它可以容纳内联元素和其他块元素,常见块元素是段落标签"p& ...

  10. python学习之路-3 初始python数据类型以及文件操作

    本篇涉及内容 set集合 函数 三元运算 文件操作 set集合 set是一个无序的且不重复的元素集合 1.创建set集合的方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...