PropertyPlaceholderConfigurer的用法:


用法1:

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

<beans

         xmlns="http://www.springframework.org/schema/beans"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns:p="http://www.springframework.org/schema/p"

         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 

<!-- 使用spring提供的PropertyPlaceholderConfigurer讀取數據庫配置信息.properties -->

         <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

                   <property name="locations">

                            <list>

                                     <!—

  1. 這里的classpath可以認為是項目中的src-
  2. 屬性名是 locations,使用子標簽<list></list>可以指定多個數據庫的配置文件,這里指定了一個

->

                                     <value>classpath:resource/config/jdbc.properties</value>

                            </list>

                   </property>

         </bean>

此時的數據庫配置文件項目路徑是這樣的

用法2:

讀取數據庫的配置文件還可以使用下面的方式

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

 

<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.0.xsd"

  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">

      <list>

         <value>/WEB-INF/config_test/jdbc.properties</value>

      </list>

    </property>

  </bean>

此時jdbc.properties文件的位置如下圖所示

.properties配置文件還可以有多個,這里在<list></list>標簽中指定了2個數據的配置文件

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

 <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.0.xsd"

  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">

      <list>

        <value>classpath:jdbc.properties</value>

        <value>/WEB-INF/config_test/jdbc.properties</value>

      </list>

    </property>

  </bean>

classpath:jdbc.properties對應的文件位置是:

文件內容是:配置的是sqlserver的連接信息

sqlserver.username=sa

sqlserver.password=sqlserver

sqlserver.url=jdbc\:jtds\:sqlserver\://localhost\:1433/J2EE

sqlserver.driver=net.sourceforge.jtds.jdbc.Driver

 /WEB-INF/config_test/jdbc.properties對應的文件位置是

文件內容是:配置的是oracle的連接信息

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

jdbc.username=jxbms

jdbc.password=jxbms

 這樣數據庫的配置信息被讀取之后,在創建datasource的時候就可以使用了

 下面連接oracle 使用apachedbcp 數據源

<bean id="dataSource"

       class="org.apache.commons.dbcp.BasicDataSource"

       destroy-method="close">

       <property name="driverClassName">

           <value>${jdbc.driverClassName}</value>

       </property>

       <property name="url">

           <value>${jdbc.url}</value>

       </property>

       <property name="username">

           <value>${jdbc.username}</value>

       </property>

       <property name="password">

           <value>${jdbc.password}</value>

       </property>

       <property name="maxActive">

           <value>100</value>

       </property>

       <property name="maxIdle">

           <value>3</value>

       </property>

       <property name="maxWait">

           <value>-1</value>

       </property>

       <property name="defaultAutoCommit">

        <value>false</value>

       </property>

    </bean>

 下面連接sqlserver數據庫使用的是c3p0數據源

<bean id="dataSource_oracle" class="com.mchange.v2.c3p0.ComboPooledDataSource"

        destroy-method="close" >

        <property name="driverClass" value="${jdbc.driverClassName}" />

        <property name="jdbcUrl" value="${jdbc.url}" />

        <property name="user" value="${jdbc.username}" />

        <property name="password"  value="${jdbc.password}" />

</bean>

 使用dbcp數據源令人郁悶的事,使用dbcpspring提供的JdbcTemplate操作數據庫是  查詢是可以的

但是執行updatedeleteinsert into 操作時,數據庫中的數據沒有變化

 從網上查詢了很多的資料,都無果。最后偶然看到網上有人說,dbcp數據源的事務不會自動提交,

當改成c3p0數據源后好了

 隨后認為這下終於可以松口氣了,誰知道天不遂人願。當更換一張表進行測試,數據庫中的數據還是沒有變化,難道c3p0數據源也不好使,

當再次經過代碼的折磨之后,

最終發現改動測試java文件,不在一個項目中,把其他的項目關閉就好了

 當文檔寫到這里時,突然發現oracle使用的dbcp數據源有這一項配置

        <property name="defaultAutoCommit">

        <value>false</value>

       </property>

原來dbcp數據源事務的自動提交功能被關閉了

 馬上把事務自動提交改成true  進行測試,一切ok(^ _ ^)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM