org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

本文介绍如何使用Spring框架中的PropertyPlaceholderConfigurer类从.properties文件加载数据库连接配置,并应用于多个数据源设置。

今天看到在.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>
     

 

 

 

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'apnsPayloadMap' defined in null: Could not resolve placeholder 'Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type' in value "${Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type' in value "${Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type}" at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:230) at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:211) at com.tplink.cloud.common.config.utils.CommonPropertyPlaceholderConfigurer.processProperties(CommonPropertyPlaceholderConfigurer.java:46) at com.tplink.cloud.msgcenter.config.PushConfig.processProperties(PushConfig.java:171) at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:291) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:167) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) at com.tplink.cloud.msgcenter.Bootstrap.initSpringContent(Bootstrap.java:123) ... 2 more Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type' in value "${Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:230) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:296) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:217) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitMap(BeanDefinitionVisitor.java:272) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:211) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:147) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85) at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:227)
最新发布
09-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值