spring中context:property-placeholder/元素
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}等类似信息。
- < bean id="propertyPlaceholderConfigurer"
- class="org.springframework.beans.factory.config.
- PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>userinfo.properties</value>
- </list>
- </property>
- < /bean>
- < bean name="userInfo" class="test.UserInfo">
- <property name="username" value="${db.username}"/>
- <property name="password" value="${db.password}"/>
- < /bean>
通过运行并分析PropertyPlaceholderConfigurerDemo示例应用,开发者能够深入理解PropertyPlaceholderConfigurer。为简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。
- < context:property-placeholder location="userinfo.properties"/>
PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,
它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。
通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。
spring中context:property-placeholder/元素的更多相关文章
- spring中context:property-placeholder/元素 转载
spring中context:property-placeholder/元素 转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...
- Spring中<context:annotation-config/>
最近在研究Spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Sp ...
- spring中 context:property-placeholder 导入多个独立的 .properties配置文件
spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 o ...
- Spring中<context:annotation-config/>的作用
spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Spring容 ...
- Spring中 <context:property-placeholder 的使用与解析 .properties 配置文件的加载
转: Spring中property-placeholder的使用与解析 Spring中property-placeholder的使用与解析 我们在基于spring开发应用的时候,一般都会将数据库的配 ...
- spring中context:property-placeholder
发现网上对于这个标签的解释过于复杂,这里从实用性角度简短的进行说明. 首先,它是spring3中提供的标签. 只需要在spring的配置文件里添加一句: <context:property-pl ...
- Spring 中context.start作用
我们经常会看到 如下代码 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configPath. ...
- 关于spring中<context:component-scan base-package="" />写法
1.通配符形式<context:component-scan base-package="com.*" /> 2.全路径 <context:component-s ...
- spring中<context:property-placeholder/>一个坑
<context:property-placeholder location="classpath:db.properties" ></context:prope ...
随机推荐
- tomcat虚拟路径的几种配置方法
一般我们都是直接引用webapps下面的web项目,如果我们要部署一个在其它地方的WEB项目,这就要在TOMCAT中设置虚拟路径了,Tomcat的加载web顺序是先加载 $Tomcat_home$\c ...
- Spring 作用域 scope
spring的作用域将对Bean的生命周期和创建方式产生影响. 主要分为五种类型的作用域 singleton (默认)在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. ...
- spring - ioc和aop
1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对象的时候,一般都是直接使用关键字类new ...
- 一个糟糕的Erlang练习题
好吧,用的语法很糟糕...但是至少是做了练习. 题目 %The sequence of triangle numbers is generated by adding the natural numb ...
- TCP/IP详解 学习四
ARP地址解析协议 当一台主机把以太网数据帧发送到位于同一局域网上的另一台主机时,是根据 48 bit的以太网地址来确定目的接口的.设备驱动程序从不检查 I P数据报中的目的 I P地址. ARP的分 ...
- POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22057 ...
- POJ2677 Tour(DP+双调欧几里得旅行商问题)
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3929 Accepted: 1761 Description ...
- CSS小记
1.元素居中 (1)水平居中:指定宽度,然后margin auto 即可 .middle{ max-width:400px; //width:400px;//当浏览器被缩小,宽度小于元素宽度时,元素会 ...
- 使用存取方法来设置Property value
对比如下代码,第一种使用了存取方法来设置,第二种直接对实例变量操作.显然我们应该采用第一种, 使用第二种情况,简单的情况还好,如果情况一旦复杂,就非常容易出错.并且直接对实例变量操作,不会引发KVO通 ...
- 【转】websocket协议规范
在线版目录: 1.引言——WebSocket协议翻译 2.一致性要求——WebSocket协议翻译 3.WebSocket URI——WebSocket协议翻译 4.打开阶段握手——WebSocket ...