`

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>
     

 

 

 

分享到:
评论
1 楼 抹去色彩 2014-12-04  
什么玩意?

相关推荐

    Spring项目中怎么配置log4j

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:log4j.properties"/&gt; ``` 这使得Spring在启动时...

    Spring中PropertyPlaceholderConfigurer的使用

    &lt;bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/spring/include/dbQuery.properties ``` 在上面的代码中,...

    ssh框架在application.xml中配置数据源所需jar

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/deploy.properties &lt;!-- 配置sessionFactory &lt;bean id="sessionFactory" class="...

    SPRING:bean配置properties

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/spring/include/dbQuery.properties ``` 这里,`propertyConfigurerForAnalysis`是`...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:proxool.properties &lt;bean id="dataSource" class="org.logicalcobwebs.proxool....

    Spring项目application.xml配置文件加解密

    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.spring...

    ssm框架整合

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:database.properties &lt;bean id="dataSource" class="org.springframework.jdbc.datasource....

    springmvc+spring+mybatis

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 配置数据源 --&gt; &lt;bean id="dataSource" class="org.apache....

    struts2 hibernate spring集成

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 使用proxool连接池配置数据源 --&gt; class="org.logicalcobwebs.proxool....

    spring 配置

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="/WEB-INF/config/jdbc.properties"/&gt; ``` 然后,配置数据源`dataSource`,这里使用了C3P0...

    关于spring系统中多系统的配置

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/spring/include/dbQuery.properties ``` 这里的`location`属性指定了属性文件的位置。`classpath:`...

    Spring属性占位符PropertyPlaceholderConfigurer的使用

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:config.properties"/&gt; ``` 在上面的代码中,`...

    spring3.2+strut2+hibernate4

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath*:jdbc.properties &lt;!-- 数据源配置,主要用于开发测试...

    SSI框架整合实例

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt; ${...

    对SAE主从数据库连接的管理和封装.docx

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:db.properties &lt;!-- 主数据库配置 --&gt; ...

Global site tag (gtag.js) - Google Analytics