今天看到在.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>
相关推荐
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:log4j.properties"/> ``` 这使得Spring在启动时...
<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:/spring/include/dbQuery.properties ``` 在上面的代码中,...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:/deploy.properties <!-- 配置sessionFactory <bean id="sessionFactory" class="...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:/spring/include/dbQuery.properties ``` 这里,`propertyConfigurerForAnalysis`是`...
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:proxool.properties <bean id="dataSource" class="org.logicalcobwebs.proxool....
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.spring...
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:database.properties <bean id="dataSource" class="org.springframework.jdbc.datasource....
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:jdbc.properties <!-- 配置数据源 --> <bean id="dataSource" class="org.apache....
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:jdbc.properties <!-- 使用proxool连接池配置数据源 --> class="org.logicalcobwebs.proxool....
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="/WEB-INF/config/jdbc.properties"/> ``` 然后,配置数据源`dataSource`,这里使用了C3P0...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:/spring/include/dbQuery.properties ``` 这里的`location`属性指定了属性文件的位置。`classpath:`...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:config.properties"/> ``` 在上面的代码中,`...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath*:jdbc.properties <!-- 数据源配置,主要用于开发测试...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:jdbc.properties <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> ${...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:db.properties <!-- 主数据库配置 --> ...