cAng^Er

          不懂我的人 , 離不了我 , 該了解了解我 !而懂我的人 , 更離不了我 , 因為他們愛我 。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 16 文章 :: 18 評論 :: 0 Trackbacks

          http://www.is.pku.edu.cn/blog/?date=2004-12-29&id=2&m=display

          DataSource注入


          xinyua

          caterpillar 對於不同的資料庫存取需求,我們使用JDBC來解決這個問題,對於不同的資料連接來源需求,Spring則提供了DataSource注入,更換資料來源只要在Bean定義檔中修改配置,而不用修改任何一行程式。 因應不同的系統,應用程式可能使用不同的資料來源,但如純綷的使用JDBC、透過連接池、或是透過JNDI等等,資料來源的更動是底層的行為,不應影響到上層的業務邏輯,為此,您可以在需要取得連接來源的Bean上保留一個資料來源注入的介面,讓依賴的資料來源由該介面注入
          對於不同的資料庫存取需求,我們使用JDBC來解決這個問題,對於不同的資料連接來源需求,Spring則提供了DataSource注入,更換資料來源只要在Bean定義檔中修改配置,而不用修改任何一行程式。

          因應不同的系統,應用程式可能使用不同的資料來源,但如純綷的使用JDBC、透過連接池、或是透過JNDI等等,資料來源的更動是底層的行為,不應影響到上層的業務邏輯,為此,可以在需要取得連接來源的Bean上保留一個資料來源注入的介面,讓依賴的資料來源由該介面注入。例如我們來寫一個簡單的Bean:

          代碼:
          package onlyfun.caterpillar;
          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          import javax.sql.DataSource;
          import java.sql.Connection;
          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          public class DataBean {
          ? ? private DataSource dataSource;
          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          ? ? public void setDataSource(DataSource dataSource) {
          ? ? ? ? this.dataSource = dataSource;
          ? ? }
          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          ? ? public void testDataSource() {
          ? ? ? ? try {
          ? ? ? ? ? ? Connection connection = dataSource.getConnection();
          ? ? ? ? ? ? if(connection != null)
          ? ? ? ? ? ? ? ? System.out.println("test ok!");
          ? ? ? ? }
          ? ? ? ? catch (Exception e) {
          ? ? ? ? ? ? e.printStackTrace();
          ? ? ? ? }
          ? ? }
          }


          這是一個簡單的測試Spring DataSource注入的程式,我們透過javax.sql.DataSource介面來注入資料來源,Spring提供了org.springframework.jdbc.datasource.DriverManagerDataSource來取得DataSource,它實作了javax.sql.DataSource,將之當作一個Bean,之後再注入DataBean中即可,Bean定義檔可以這麼撰寫:
          代碼:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
          <beans>
          ? ? <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          ? ? ? ? <property name="driverClassName">
          ? ? ? ? ? ? <value>com.mysql.jdbc.Driver</value>
          ? ? ? ? </property>
          ? ? ? ? <property name="url">
          ? ? ? ? ? ? <value>jdbc:mysql://localhost:3306/TestDB</value>
          ? ? ? ? </property>
          ? ? ? ? <property name="username">
          ? ? ? ? ? ? <value>caterpillar</value>
          ? ? ? ? </property>
          ? ? ? ? <property name="password">
          ? ? ? ? ? ? <value>123456</value>
          ? ? ? ? </property>
          ? ? </bean>
          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          ? ? <bean id="dataBean" class="onlyfun.caterpillar.DataBean">
          ? ? ? ? <property name="dataSource">
          ? ? ? ? ? ? <ref bean="dataSource"/>
          ? ? ? ? </property>
          ? ? </bean>
          </beans>


          如果之前只使用spring-core.jar這個類別庫,還必須加入spring-dao.jar,org.springframework.jdbc.datasource.DriverManagerDataSource是包括在這個類別庫中,如果使用的是spring.jar,當中已經包括了,無需加入任何的jar,當然,為了使用JDBC,必須要有JDBC驅動程式的jar檔。

          可以用下面這段程式簡單的測試一下:
          代碼:
          BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
          XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);
          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          reader.loadBeanDefinitions(new ClassPathResource("bean.xml"));;
          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          BeanFactory bf = (BeanFactory) reg;
          DataBean dataBean = (DataBean) bf.getBean("dataBean");
          dataBean.testDataSource();


          DriverManagerDataSource並沒有提供連接池的功能,只能作作簡單的單機連接測試,現在假設連接測試沒有問題了,想要換上DBCP以獲得連接池的功能,則原程式不用更動,只要改改Bean定義檔就可以了:
          代碼:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
          <beans>
          ? ? <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
          ? ? ? ? <property name="driverClassName">
          ? ? ? ? ? ? <value>com.mysql.jdbc.Driver</value>
          ? ? ? ? </property>
          ? ? ? ? <property name="url">
          ? ? ? ? ? ? <value>jdbc:mysql://localhost:3306/TestDB</value>
          ? ? ? ? </property>
          ? ? ? ? <property name="username">
          ? ? ? ? ? ? <value>caterpillar</value>
          ? ? ? ? </property>
          ? ? ? ? <property name="password">
          ? ? ? ? ? ? <value>123456</value>
          ? ? ? ? </property>
          ? ? </bean>
          ? ? <bean id="dataBean" class="onlyfun.caterpillar.DataBean">
          ? ? ? ? <property name="dataSource">
          ? ? ? ? ? ? <ref bean="dataSource"/>
          ? ? ? ? </property>
          ? ? </bean>
          </beans>


          現在我們使用的是org.apache.commons.dbcp.BasicDataSource作為注入的DataSource源,為了使用DBCP的功能,必須要將commons-dbcp.jar加入CLASSPATH中,另外還需要commons-pool.jar與commons-collections.jar,這些都可以在Spring的相依版本中的lib目錄下找到。

          注意到我們在dataSource Bean上宣告了destroy-method,如此可以確保BeanFactory在關閉時也一併關閉BasicDataSource。

          如果Servlet容器提供了JNDI資料源,也可以簡單的換上這個資料源:
          代碼:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
          <beans>
          ? ? <bean id="dataSource" class="org.springframework.indi.JndiObjectFactoryBean">
          ? ? ? ? <property name="jndiName">
          ? ? ? ? ? ? <value>jdbc/TestDB</value>
          ? ? ? ? </property>
          ? ? </bean>
          ? ? <bean id="dataBean" class="onlyfun.caterpillar.DataBean">
          ? ? ? ? <property name="dataSource">
          ? ? ? ? ? ? <ref bean="dataSource"/>
          ? ? ? ? </property>
          ? ? </bean>
          </beans>


          為了使用org.springframework.indi.JndiObjectFactoryBean,必須加入spring-context.jar這個類別庫,jndiName實際上要根據所設定的JNDI查詢名稱
          posted on 2007-01-15 20:03 cAng^Er 閱讀(612) 評論(0)  編輯  收藏 所屬分類: |:Web 框架:|

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 霍邱县| 菏泽市| 漾濞| 通渭县| 屯昌县| 印江| 宾川县| 广汉市| 资中县| 都匀市| 屯昌县| 郓城县| 台安县| 和硕县| 小金县| 武陟县| 平陆县| 凤山县| 三河市| 凌云县| 滦南县| 珠海市| 油尖旺区| 九寨沟县| 三门县| 盐源县| 高碑店市| 邹平县| 郁南县| 读书| 台安县| 牡丹江市| 南和县| 康平县| 青岛市| 界首市| 日照市| 伊春市| 海口市| 上蔡县| 盐城市|