在Spring配置文件中使用properties文件

使用Spring提供的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer可以引入properties文件,并且在applectionContext文件的其他部分用${}引用其中的值。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
  lazy-init="false">
  <property name="locations">
   <list>
	<value>classpath*:dao.properties</value>
   </list>
  </property>
</bean>

而使用org.springframework.beans.factory.config.PropertyOverrideConfigurer可以用properties文件中的值覆盖applicationContext中定义的bean的property值,例如:

//原来的bean中的一个属性,通过${jdbc.url}引入dao.properties中的一个值
<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="jdbcUrl">
			<value>${jdbc.url}</value>
		</property>
</bean>
//利用PropertyOverrideConfigurer,引入另一个properties文件test.properties
 <bean id="testPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
        <property name="location" value="classpath:context/test/test.properties"/>
        <property name="ignoreInvalidKeys" value="true"/>
 </bean>
//test.properties中覆盖原来的dataSource的jdbcUrl属性:
dataSource.jdbcUrl=jdbc:hsqldb:res:db

将testPropertyConfigurer放在单独的xml文件中,如果不加载这个xml,那么dataSource.jdbcUrl为dao.properties中的值
如果加载这个文件,那么dataSource.jdbcUrl为test.properties中的值

以上参考springside.com.cn

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值