spring中context:property-placeholder/元素 转载
spring中context:property-placeholder/元素 转载
1.有些参数在某些阶段中是常量
比如 :a、在开发阶段我们连接数据库时的连接url,username,password,driverClass等
b、分布式应用中client端访问server端所用的server地址,port,service等
c、配置文件的位置
2.而这些参数在不同阶段之间又往往需要改变
比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况。
期望:能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息
解决:spring3中提供了一种简便的方式就是context:property-placeholder/元素
只需要在spring的配置文件里添加一句:<context:property-placeholder 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: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/元素
1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的 ...
- 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 ...
随机推荐
- listview中button抢占焦点问题
解决办法Item xml 根节点添加 android:descendantFocusability="blocksDescendants" Button 设置 android:fo ...
- 从source folder 下将其所有子文件夹的*.* 文件拷贝到 target folder (不拷贝文件夹名仅拷贝文件)
因本人较懒,一直认为电脑能做的就让电脑来做,所以写下这个批处理的小脚本方便工作. 场景:碰到要拷贝一个文件夹(source folder)下的多个子文件夹(sub-folder)的文件到指定文件夹下( ...
- jvm - 内存结构以其解析
可以将jvm粗略分为以下部分: Heap Memory:存储java对象. Non-Heap Memory:存储加载的class文件,以及其他meta-data信息. Other:存储java代码,j ...
- shell脚本学习之Bash shell 里各种括号的用法
今天在 SegmentFault 上看到又有人问起关于Shell里各种括号的问题.对于很多玩Shell的人,括号是个很尴尬的问题,用起来没问题,说起来不明白,我在这里总结一下Bash Shell几种括 ...
- swift 截取字符串
- jQuery 遍历后代
后代是子.孙.曾孙等等. 通过 jQuery,您能够向下遍历 DOM 树,以查找元素的后代. 向下遍历 DOM 树 下面是两个用于向下遍历 DOM 树的 jQuery 方法: children() f ...
- java_设计模式_状态模式_State Pattern(2016-08-16)
定义: 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 类图: 状态模式所涉及到的角色有: ● 环境(Context)角色,也成上下文:定义客户端所感兴趣的接口,同时维护一个 ...
- c语言字符数组与字符串的使用详解
转自:http://www.jb51.net/article/37456.htm 1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ ...
- 343. Integer Break -- Avota
问题描述: Given a positive integer n, break it into the sum of at least two positive integers and maximi ...
- 谷歌的C++智能指针实现
//智能指针基类所有智能指针对象都继承该类class RefCountedBase { public: ; ; protected: virtual ~RefCountedBase(){} }; 智能 ...