Spring属性文件的覆盖配置:uses of PropertyOverrideConfigurer
来源:互联网 发布:怎样用命令压缩linux下 编辑:程序博客网 时间:2024/04/30 15:46
Spring提供了用属性文件配置Spring的功能,方法是使用PropertyPlaceholderConfigurer加载配置文件:
xml 代码
- <!-- RESOURCE DEFINITIONS -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath*:conf/jdbc.properties</value>
- <value>classpath*:conf/hibernate.properties</value>
- </list>
- </property>
- </bean>
加载了配置文件以后,书写占位符 ${property.name}来配置bean的属性,例如
xml 代码
- <!-- DataSource Definition, using Apache DBCP connection pool -->
- <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>
在实际开发中,当我们需要不同的配置策略的时候,比如开发和测试很可能用的是不同的数据库,就需要更改配置文件,很麻烦。幸好Spring提供了解觉的办法:PropertyOverrideConfigurer,顾名思义,它允许配置文件加载后动态的覆盖某些bean的属性,当没有显式配置的时候使用默认的配置,否则使用覆盖配置。
具体用法:
xml 代码
- <bean id="testPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
- <property name="location" value="classpath:spring/test/test.properties"/>
- <property name="ignoreInvalidKeys" value="true"/>
- </bean>
test.properties是一个属性文件,但是具体的写法不一样,PropertyPlaceholderConfigurer中属性文件的key写的是占位符的变量名,而这里是按照bean.property=value的格式来写的,比如你想覆盖datasource的driverClassName属性值,写成dataSource.driverClassName=jdbc:mysql:....
注意:对于属性集合的情况,有特殊的写法
xml 代码
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
- <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
- <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
- </props>
- </property>
- </bean>
hibernate.dialect是属性集合hibernateProperties中的一项,如果写成sessionFactory.hibernateProperties.hibernate.dialect就会报错,这时候要写成sessionFactory.hibernateProperties[hibernate.dialect],这一点,Spring的任何文档里都没有提及,是被我给蒙出来的,吼吼^ ^
- Spring属性文件的覆盖配置:uses of PropertyOverrideConfigurer
- spring的PropertyPlaceholderConfigurer和PropertyOverrideConfigurer
- Spring占位符PropertyOverrideConfigurer的使用
- Spring占位符PropertyOverrideConfigurer的使用
- spring中PropertyPlaceholderConfigurer和PropertyOverrideConfigurer的使用
- spring-PropertyPlaceholderConfigurer和PropertyOverrideConfigurer
- AndroidManifest配置之uses-feature---glEsVersion属性
- Spring Xml 文件的配置 参数 属性 说明
- 简化Spring的xml文件配置-自动装配Bean属性
- Spring Xml 文件的配置 参数 属性 说明
- Spring Xml 文件的配置 参数 属性 说明
- Spring的两种容器后处理器(PropertyPlaceholderConfigurer和PropertyOverrideConfigurer)
- spring学习5.2-PropertyPlaceholderConfigurer 和PropertyOverrideConfigurer
- Spring(14)——PropertyOverrideConfigurer
- 关于系统中使用多个PropertyPlaceholderConfigurer的配置及PropertyOverrideConfigurer
- 关于系统中使用多个PropertyPlaceholderConfigurer的配置及PropertyOverrideConfigurer
- Spring配置中引用外部属性文件
- Spring通过.properties文件引入属性配置
- 行业网站: 权威性与高度专业化系列 网盛科技的海外拓展之路(转载自:全球品牌网)
- Scott Mitchell 的ASP.NET 2.0数据教程之62:创建一个用户自定义的Database-Driven Site Map Provider
- JavaScript梅花雨控件
- Unix高级安全设置第三部分-LINUX系列
- Iframe框架中父子窗体的互相控制
- Spring属性文件的覆盖配置:uses of PropertyOverrideConfigurer
- VMware上网配置
- Brief contents----简要目录
- [ASP.NET] Session 详解
- Spring常用配置文件
- vim技巧整理
- VC中实现最小二乘法 直线拟合 Y=a0+a1X 以及 Y=aX
- c语言编程小技巧:c语言小技巧:枚举
- 上班第一周记