uses of PropertyOverrideConfigurer
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的任何文档里都没 有提及。
分享到:
相关推荐
The Uses of Big Data in Cities.pdf
This sample shows different uses of Restriction Operators. LINQ - Join Operators This sample shows different uses of Join Operators LINQ - Projection Operators This sample shows different uses...
This intriguing, accessible work leads readers to an excellent grasp of the fundamental notions of infinity used in the calculus and in virtually all other mathematical disciplines. Each chapter's ...
ARM的C语言例程: This example shows how you can use signal functions in the 礦ision2 debugger to simulate a signal that is coming into one of the analog inputs of the AT91M55800A.
Uses of temporal logic and data-flow-analysis techniques have also made model checking more naturally efficient. All these contributions have helped solve the ever-present “state explosion problem....
Beginning with coverage of the history of the architecture of Web applications, highlighting the uses of the standard web API to create applications with increasingly sophisticated architectures, ...
s market, business students must have a solid understanding of derivatives concepts and instruments and the uses of those instruments in corporations. The Second Edition has an accessible mathematical...
### 关于core_uses_pid标志对多线程程序无用的调查 #### 案例背景及问题描述 在RHEL5.6系统环境下,当一个进程启动并使用线程时,若出现core dump(核心转储),所生成的core文件名会包含进程ID。尽管在系统中设置...
The Handbook of Applied Multivariate Statistics and Mathematical Modeling explains the appropriate uses of multivariate procedures and mathematical modeling techniques, and prescribe practices that ...
The system uses neural representations to sepa- rate and recombine content and style of arbitrary images, providing a neural algorithm for the creation of artistic images. Moreover, in light of the ...
uses file to extract data and takes input of code. searches for the item in file. basic file readin and search文件读写查询
The design uses a configurable Weighted Overlap-Add (WOLA) filterbank coprocessor to implement the analysis and synthesis filterbanks. A new method to convert the two-times over-sampled, complex ...
An example of the classic game pong...it uses bitblt for displaying and can be very fun. The only known problem is that the gameplay is based on the cpu speed
资源名:Uses the Gray-Scott system of PDE's to create beautiful Turing pattern animations. 资源类型:matlab项目全套源码 源码说明: 全部项目源码都是经过测试校正后百分百成功运行的,如果您下载后不能运行...
The chapter on tree drawing uses R's powerful graphical environment. A section deals with the analysis of diversification with phylogenies, one of the author's favorite research topics. The last ...
Uses: The class Life and its methods initialize(), print(), and update(). The functions instructions(), user_says_yes(). */ { Life configuration; instructions(); configuration.initialize(); // if...
The language is C++.Specifically, the font size is larger, and the page size is 7"x10" (the regular format uses 6"x9"). The content is identical. The sampler should give you a very good idea of the...
Uses the Running Operation as the Main Thread Difficulty in understanding an operating system (OS) lies not in the technical aspects, but in the complex relationships inside the operating ...
a method that uses long term point trajectories based on dense optical flow. Defining pair-wise distances between these trajectories allows to cluster them, which results in temporally consistent ...