关于PropertyPlaceholderConfigurer类(srping)

 

Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,例如:

    <bean id="propertyConfigurer"
          class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
<property name="location" value="classpath:config/jdoserver.properties"/>
    
</bean>


但是好像在属性文件定义中却不支持多个属性文件的定义,比如不能这样用config/*.properties

经过查看源码,发现可以使用locations属性定义多个配置文件:

<property name="locations">
            
<list>
                
<value>classpath:config/maxid.properties</value>
                
<value>classpath:config/jdoserver.properties</value>
            
</list>
</property>


使用外部属性后如下:

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
<property name="driverClassName" value="${jdbc.agent.driver}"/>
        
<property name="url" value="${jdbc.agent.main.url}"/>
    
</bean>


上面的例子是采用Spring DriverManagerDataSource包装VJDBC Connection的用法,没有提供连接池功能,只能作作简单的单机连接。

 

一个实际例子:

 

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!-- This is beans.xml for springsample -->

 

<beans>

    <bean id="DataSourcePpgweb"

       class="org.springframework.jdbc.datasource.DriverManagerDataSource">

 

       <property name="driverClassName">

           <value>${db.driver}</value>

       </property>

       <property name="url">

           <value>${db.ppgweb.url}</value>

       </property>

 

       <property name="username">

           <value>${db.ppgweb.username}</value>

       </property>

       <property name="password">

           <value>${db.ppgweb.password}</value>

       </property>

    </bean>

    <!-- PlatformTransactionManager -->

    <bean id="transactionManager"

       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       <property name="dataSource">

           <ref bean="DataSourcePpgweb" />

       </property>

    </bean>

 

    <bean id="DivisionDao"

       class="com.polyplastics.ppgweb.epterms.dbaccess.dao.impl.DivisionDaoImpl">

       <property name="dataSource">

           <ref bean="DataSourcePpgweb" />

       </property>

    </bean>

    <bean id="TermDao"

       class="com.polyplastics.ppgweb.epterms.dbaccess.dao.impl.TermDaoImpl">

       <property name="dataSource">

           <ref bean="DataSourcePpgweb" />

       </property>

    </bean>

   

    <bean id="dbConf"

       class="org.springframework.core.io.ClassPathResource">

       <constructor-arg>

           <value>db.properties</value>

       </constructor-arg>

    </bean>

   

    <bean id="eptermsConf"

       class="org.springframework.core.io.ClassPathResource">

       <constructor-arg>

           <value>eptermsConfig.properties</value>

       </constructor-arg>

    </bean>

 

    <bean id="dbConfPostProcessor"

       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

       <property name="locations">

           <list>

              <ref bean="dbConf" />

              <ref bean="eptermsConf" />

           </list>

       </property>

    </bean>

</beans>

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值