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

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应用

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

  3. Spring 利用PropertyPlaceholderConfigurer占位符

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

  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. Nginx动静分离基本概述

    Nginx动静分离基本概述 动静分离,通过中间将动静分离和静态请求进行分离: 通过中间件将动态请求和静态请求分离,可以建上不必要的请求消耗,同时能减少请求的延时. 通过中间件将动态请求和静态请求分离, ...

  2. JDBC中如何进行事务处理?

    Connection提供了事务处理的方法,通过调用setAutoCommit(false)可以设置手动提交事务:当事务完成后用commit()显式提交事务:如果在事务处理过程中发生异常则通过rollb ...

  3. 修改Docker中容器的时间和宿主主机时间一致

    修改Docker容器的时间和宿主时间一致 在查看容器的日志的,发现时间有和宿主主机时间相差有8个小时,而且宿主主机使用的是CST时间,容器容器使用的是UTC时间 主机时间 DOCKER容器的时间 世界 ...

  4. css实现下拉框导航条

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  5. C语言——enum

    #include<stdio.h> enum Season { spring, summer=100, fall=96, winter }; typedef enum { Monday, ...

  6. 【leetcode】984. String Without AAA or BBB

    题目如下: Given two integers A and B, return any string S such that: S has length A + B and contains exa ...

  7. 百度链接提交主动推送 c#实现

    说明:此方法适合百度站长,利用此方法可以第一时间将你的原创文章上传到百度,保护你的著作权,也可以帮你的网站进行引流 以下是代码实现: /// <summary>         /// 提 ...

  8. Python--数据类型与变量(列表、元祖、字典)

    今天我们来看Python中3种內建的数据结构:列表.元祖和字典 列表 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素特性:1.可存放多个值2.可修改指定索引位置对应的值,可变 ...

  9. 左闭右开线段树 2019牛客多校(第七场)E_Find the median(点代表区间

    目录 题意 一种解析 AC_Code @(2019第七场牛客 E_Find the median 左闭右开线段树) 题意 链接:here 我理解的题意就是:初始序列为空,有\(n(400000)\)次 ...

  10. python join函数

    join()函数 语法:  'sep'.join(seq) 参数说明sep:分隔符.可以为空seq:要连接的元素序列.字符串.元组.字典上面的语法即:以sep作为分隔符,将seq所有的元素合并成一个新 ...