1.有些参数在某些阶段中是常量

比如 :a、在开发阶段我们连接数据库时的连接url,username,password,driverClass等

b、分布式应用中client端访问server端所用的server地址,port,service等

c、配置文件的位置

2.而这些参数在不同阶段之间又往往需要改变

比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况。

期望:能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息

解决:spring3中提供了一种简便的方式就是context:property-placeholder/元素  

只需要在spring的配置文件里添加一句location="classpath:jdbc.properties"/> 即可,这里location值为参数配置文件的位置,参数配置文件通常放在src目录下,而参数配置文件的格式跟java通用的参数配置文件相同,即键值对的形式,例如:

#jdbc配置

test.jdbc.driverClassName=com.mysql.jdbc.Driver
test.jdbc.url=jdbc:mysql://localhost:3306/test
test.jdbc.username=root
test.jdbc.password=root

行内#号后面部分为注释

应用:

1.这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource

在配置文件里这么定义bean:

<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   < property name="driverClassName" value="${test.jdbc.driverClassName}"/>
   < property name="url" value="${test.jdbc.url}"/>
   < property name="username" value="${test.jdbc.username}"/>
   < property name="password" value="${test.jdbc.password}"/>
</bean>

2.甚至可以将${ }这种形式的变量用在spring提供的注解当中,为注解的属性提供值

---------------------------------------------------------  

外在化应用参数的配置

在开发企业应用期间,或者在将企业应用部署到生产环境时,应用依赖的很多参数信息往往需要调整,比如LDAP连接、RDBMS JDBC连接信息。对这类信息进行外在化管理显得格外重要。PropertyPlaceholderConfigurer和PropertyOverrideConfigurer对象,它们正是担负着外在化配置应用参数的重任。????

 < context:property-placeholder/>元素

PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,它能够对<bean/>中的属性值进行外在化管理。开发者可以提供单独的属性文件来管理相关属性。比如,存在如下属性文件,摘自userinfo.properties。
db.username=scott
db.password=tiger

如下内容摘自propertyplaceholderconfigurer.xml。正常情况下,在userInfo的定义中不会出现${db.username}、${db.password}等类似信息,这里采用PropertyPlaceholderConfigurer管理username和password属性的取值。DI容器实例化userInfo前,PropertyPlaceholderConfigurer会修改userInfo的元数据信息(<bean/>定义),它会用userinfo.properties中db.username对应的scott值替换${db.username}、db.password对应的tiger值替换${db.password}。最终,DI容器在实例化userInfo时,UserInfo便会得到新的属性值,而不是${db.username}、${db.password}等类似信息。


  1. < bean id="propertyPlaceholderConfigurer"
  2. class="org.springframework.beans.factory.config.
  3. PropertyPlaceholderConfigurer">
  4. <property name="locations">
  5. <list>
  6. <value>userinfo.properties</value>
  7. </list>
  8. </property>
  9. < /bean>
  10. < bean name="userInfo" class="test.UserInfo">
  11. <property name="username" value="${db.username}"/>
  12. <property name="password" value="${db.password}"/>
  13. < /bean>

通过运行并分析PropertyPlaceholderConfigurerDemo示例应用,开发者能够深入理解PropertyPlaceholderConfigurer。为简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。


  1. < context:property-placeholder location="userinfo.properties"/>

PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,

它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。

通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。

spring中context:property-placeholder/元素的更多相关文章

  1. spring中context:property-placeholder/元素 转载

    spring中context:property-placeholder/元素  转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...

  2. Spring中<context:annotation-config/>

    最近在研究Spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Sp ...

  3. spring中 context:property-placeholder 导入多个独立的 .properties配置文件

    spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 o ...

  4. Spring中<context:annotation-config/>的作用

    spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Spring容 ...

  5. Spring中 <context:property-placeholder 的使用与解析 .properties 配置文件的加载

    转: Spring中property-placeholder的使用与解析 Spring中property-placeholder的使用与解析 我们在基于spring开发应用的时候,一般都会将数据库的配 ...

  6. spring中context:property-placeholder

    发现网上对于这个标签的解释过于复杂,这里从实用性角度简短的进行说明. 首先,它是spring3中提供的标签. 只需要在spring的配置文件里添加一句: <context:property-pl ...

  7. Spring 中context.start作用

    我们经常会看到 如下代码 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configPath. ...

  8. 关于spring中<context:component-scan base-package="" />写法

    1.通配符形式<context:component-scan base-package="com.*" /> 2.全路径 <context:component-s ...

  9. spring中<context:property-placeholder/>一个坑

    <context:property-placeholder location="classpath:db.properties" ></context:prope ...

随机推荐

  1. WebService学习过程中的心得和问题

    1.发布一个WebService 2.调用第三方提供的WebService服务

  2. 搭建OpenWrt开发环境(包括编译过程)

    OpenWrt是一个高度模块化.高度自动化的嵌入式linux发行版,其编译和安装过程比普通的linux发行版而言,要简单太多了.如果您是新手,您那恐惧的心大可放到肚子里,呵呵.对于新手来说最麻烦的恐怕 ...

  3. shell--题目

    1.有一个文件,里面有二列,第一列ip地址,第二列是时间,同一个ip可能出现多次,但时间不同. 文件类似下面的样子: 192.168.1.2              13:10 192.127.12 ...

  4. python 学习笔记2(list/directory/文件对象/模块/参数传递)

    ### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象. 11. list list是一个类.每个列表都属于该类. >>>nl = [1,2,5,3, ...

  5. BZOJ-1800 飞行棋 数学+乱搞

    这道题感觉就是乱搞,O(n^4)都毫无问题 1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1172 So ...

  6. MyEclipse10中导入的jquery文件报错(出现红叉叉,提示语法错误)

    为了做一个页面特效,导入了一个jQuery文件,怎想,myeclipse竟然报错说是语法错误,但是这个js文件我是从官网上下载的,不应该出错才对,百度谷歌之后终于找到了解决办法: 选中报错的js文件, ...

  7. 【poj2983】 Is the Information Reliable?

    http://poj.org/problem?id=2983 (题目链接) 一个SB错误TLE了半个小时... 题意 一条直线上有n个点,给出m条信息,若为P则表示点A在点B的北方X米,若为V则表示A ...

  8. codevs3196 黄金宝藏

    题目描述 Description 小毛终于到达宝藏点,他意外地发现有一个外星人(名叫Pluto).宝藏是一些太空黄金,有n堆排成一行,每堆中有xi颗黄金.小毛和Pluto决定轮流从中取出黄金,规则是每 ...

  9. File类的创建,删除文件

    File.Create(@"C:\Users\shuai\Desktop\new.txt"); Console.WriteLine("创建成功"); Conso ...

  10. WebBrowser 禁用右键

    禁用错误脚本提示 将 WebBrowser控件的 ScriptErrorsSuppressed 设为 true 禁用右键菜单 将 WebBrowser 的 IsWebBrowserContextMen ...