org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

今天看到在.properties文件里面配置多个数据库链接等信息,然后当作常量取值。这样对于在系统多个地方用到而且可能会变动的变量值配置在.properties文件里面就比较简便了。主要是由spring框架的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类在初始化的时候加载此配置文件。通过这个类,您可以将一些组态设定,移出至.properties文件中,而.properties文件可以作为客户根据需求,自定义一些相关的参数。
来看一个Bean定义档的实际例子:

 

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>classpath:db.properties</value>
		</property>
	</bean> 
	
	<bean id="dictionaryLoad" class="com.bsoft.transfer.utils.DictionaryLoad" />

	<bean id="dataSource_Target" class="org.apache.commons.dbcp.BasicDataSource">  
	  <property name="driverClassName">  
	    <value>${target.database.driver}</value>  
	  </property>  
	  <property name="username">  
	    <value>${target.database.user}</value>  
	  </property>  
	  <property name="password">  
	    <value>${target.database.password}</value>  
	  </property>  
	  <property name="url">  
	    <value>${target.database.url}</value>  
	  </property> 
	  <property name="validationQuery" value="select 1 "/>   
	</bean>
	
	<bean id="dataSource_Source" class="org.apache.commons.dbcp.BasicDataSource">  
	  <property name="driverClassName">  
	    <value>${source.bschis.database.driver}</value>  
	  </property>  
	  <property name="username">  
	    <value>${source.bschis.database.user}</value>  
	  </property>  
	  <property name="password">  
	    <value>${source.bschis.database.password}</value>  
	  </property>  
	  <property name="url">  
	    <value>${source.bschis.database.url}</value>  
	  </property>
	  <property name="validationQuery" value="select 1 from dual"/>  
	</bean> 
	
	<bean id="dataSource_Source_His" class="org.apache.commons.dbcp.BasicDataSource">  
	  <property name="driverClassName">  
	    <value>${source.his.database.driver}</value>  
	  </property>  
	  <property name="username">  
	    <value>${source.his.database.user}</value>  
	  </property>  
	  <property name="password">  
	    <value>${source.his.database.password}</value>  
	  </property>  
	  <property name="url">  
	    <value>${source.his.database.url}</value>  
	  </property>  
	  <property name="validationQuery" value="select 1 from dual"/>
	</bean>
</beans>

 

 

db.properties:

source.bschis.database.url=jdbc:oracle:thin:@ZERO-PC:1521:SDBSCHIS
source.bschis.database.driver=oracle.jdbc.driver.OracleDriver
source.bschis.database.user=sdwsj
source.bschis.database.password=bsoft

source.his.database.url=jdbc:oracle:thin:@ZERO-PC:1521:FSMIDDB
source.his.database.driver=oracle.jdbc.driver.OracleDriver
source.his.database.user=fswsj
source.his.database.password=bsoft

target.database.url=jdbc:mysql://ZERO-PC:3306/posdb
target.database.driver=com.mysql.jdbc.Driver
target.database.user=root
target.database.password=bsoft

 

如果有多个.properties文件,则可以透过  locations   属性来设定,

  • <property name="locations">
      <list>
        <value>classpath:db.properties</value>
        <value>classpath:db2.properties</value>
      </list>
    </property>
     

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值