关于PropertyPlaceholderConfigurer类(srping)

来源:互联网 发布:淘宝客服回答模板 编辑:程序博客网 时间:2024/05/16 14:21
 

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>

 

 

原创粉丝点击