在Spring3中,配置DataSource的方法

來源:互聯網
上載者:User

在Spring3中,配置DataSource的方法有五種。


第一種:beans.xml

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">          <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />          <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=spring" />          <property name="username" value="sa" />          <property name="password" value="********" />      </bean>  

第二種:beans.xml
  

<bean id="mappings"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">            <property name="locations" value="classpath:jdbc.properties"></property>     </bean>   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">        <property name="driverClassName" value="${jdbc.driverClassName}" />        <property name="url" value="${jdbc.url}" />        <property name="username" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />  </bean>  


在src檔案夾裡建立一個jdbc.properties檔案,裡面的內容為如下:

    jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver      jdbcjdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=spring      jdbc.username=sa      jdbc.password=********  


第三種:beans.xml

<bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">      <property name="locations" value="classpath:jdbc.properties"></property>   </bean>   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">      <property name="driverClassName" value="${jdbc.driverClassName}" />      <property name="url" value="${jdbc.url}" />      <property name="username" value="${jdbc.username}" />       <property name="password" value="${jdbc.password}" />  </bean>         <context:property-placeholder location="classpath:jdbc.properties" />        

在src檔案夾裡建立一個jdbc.properties檔案,裡面的內容為如下:  

jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver      jdbcjdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=spring      jdbc.username=sa      jdbc.password=******** 

其中第二種與第三種類似,只是指定設定檔的方法不一樣。

第四種:beans.xml

    <bean id="mappings"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">           <!-- typed as a java.util.Properties -->       <property name="properties">          <value>             jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver             jdbcjdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=spring             jdbc.username=sa             jdbc.password=********          </value>       </property>      </bean>                <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">              <property name="driverClassName" value="${jdbc.driverClassName}" />              <property name="url" value="${jdbc.url}" />              <property name="username" value="${jdbc.username}" />              <property name="password" value="${jdbc.password}" />          </bean>  

第五種:beans.xml

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close"        p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"        p:url="jdbc:sqlserver://localhost:1433;DatabaseName=spring"        p:username="sa"        p:password="********"/>  

再加上命名空間:

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

績效參數要根據實際情況測試得來的資料確定如何配置。
轉自:http://www.cppblog.com/fenglin/articles/130494.html

第六種,最近發在網上看到

spring3中提供了一種簡便的方式就是context:property-placeholder/元素
只需要在spring的設定檔裡添加一句:

<context:property-placeholder location="classpath:jdbc.properties"/>  

即可,這裡location值為參數設定檔的位置,參數設定檔通常放在src目錄下,而參數設定檔的格式跟java通用的參數設定檔相同,即索引值對的形式,例如:

#jdbc配置test.jdbc.driverClassName=com.mysql.jdbc.Driver  test.jdbc.url=jdbc:mysql://localhost:3306/test  test.jdbc.username=root  test.jdbc.password=root 

行內#號後面部分為注釋
應用:
1.這樣一來就可以為spring配置的bean的屬性設定值了,比如spring有一個jdbc資料來源的類DriverManagerDataSource
在設定檔裡這麼定義bean:

<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">      <property name="driverClassName" value="${test.jdbc.driverClassName}"/>      <property name="url" value="${test.jdbc.url}"/>      <property name="username" value="${test.jdbc.username}"/>      <property name="password" value="${test.jdbc.password}"/>  </bean> 

2.甚至可以將${ }這種形式的變數用在spring提供的註解當中,為註解的屬性提供值
外在化應用參數的配置

在開發公司專屬應用程式期間,或者在將公司專屬應用程式部署到生產環境時,應用依賴的很多參數資訊往往需要調整,比如LDAP串連、RDBMS JDBC串連資訊。對這類資訊進行外在化管理顯得格外重要。PropertyPlaceholderConfigurer和PropertyOverrideConfigurer對象,它們正是擔負著外在化配置應用參數的重任。

  <context:property-placeholder/>元素PropertyPlaceholderConfigurer實現了BeanFactoryPostProcessor介面,它能夠對<bean/>中的屬性值進行外在化管理。開發人員可以提供單獨的屬性檔案來管理相關屬性。比如,存在如下屬性檔案,摘自userinfo.properties。

Properties代碼

db.username=scott  db.password=tiger 

如下內容摘自propertyplaceholderconfigurer.xml。正常情況下,在userInfo的定義中不會出現${db.username}、${db.password}等類似資訊,這裡採用PropertyPlaceholderConfigurer管理username和password屬性的取值。DI容器執行個體化userInfo前,PropertyPlaceholderConfigurer會修改userInfo的中繼資料資訊(<bean/>定義),它會用userinfo.properties中db.username對應的scott值替換${db.username}、db.password對應的tiger值替換${db.password}。最終,DI容器在執行個體化userInfo時,UserInfo便會得到新的屬性值,而不是${db.username}、${db.password}等類似資訊。

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>userinfo.properties</value>            </list>        </property>    </bean>       <bean name="userInfo" class="test.UserInfo">      <property name="username" value="${db.username}"/>      <property name="password" value="${db.password}"/>    </bean>  

通過運行並分析PropertyPlaceholderConfigurerDemo樣本應用,開發人員能夠深入理解PropertyPlaceholderConfigurer。為簡化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面給出了配置樣本,啟用它後,開發人員便不用配置PropertyPlaceholderConfigurer對象了。

<context:property-placeholder location="userinfo.properties"/>
PropertyPlaceholderConfigurer內建的功能非常豐富,如果它未找到${xxx}中定義的xxx鍵,它還會去JVM系統屬性(System.getProperty())和環境變數(System.getenv())中尋找。通過啟用systemPropertiesMode和searchSystemEnvironment屬性,開發人員能夠控制這一行為。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.