隨筆-2  評(píng)論-2  文章-0  trackbacks-0
          對(duì)于不同的數(shù)據(jù)庫(kù)存取需求,我們使用JDBC來解決這個(gè)問題,對(duì)于不同的數(shù)據(jù)庫(kù)連接來源需求,Spring則提供了DataSource注入,更換數(shù)據(jù)庫(kù)連接來源只要在Bean.xml中修改配置,而不用修改任何一行程序。 

                  因應(yīng)用不同的系統(tǒng),應(yīng)用程序可能使用不同的數(shù)庫(kù)據(jù)連接來源,但如純粹的使用JDBC、透過連接池、或是透過JNDI等等,數(shù)據(jù)庫(kù)連接來源 的更改是底層的行為,不應(yīng)影響到上層的業(yè)務(wù)邏輯,為此,您可以在需要取得連接來源的Bean上保留一個(gè)資料來源注入的接口,讓依賴的資料來源由該接口注 入。例如我們來寫一個(gè)簡(jiǎn)單的

          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(); 
                  } 
              } 



              這是一個(gè)簡(jiǎn)單的測(cè)試Spring DataSource注入的程式,我們通過javax.sql.DataSource接口來注入資料來源, Spring提供了org.springframework.jdbc.datasource.DriverManagerDataSource來取得 DataSource,它實(shí)現(xiàn)了javax.sql.DataSource,您將之當(dāng)作一個(gè)Bean,之后再注入DataBean中即可, Bean.xml可以這么寫: 
          代碼: 
          <?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這個(gè)類庫(kù),您還必須加入spring-dao.jar,
          org.springframework.jdbc.datasource.DriverManagerDataSource是包括在這個(gè)類庫(kù)中,如果您 使用的是spring.jar,當(dāng)中已經(jīng)包括了,無(wú)需加入任何的jar,當(dāng)然,為了使用JDBC,您必須要有JDBC驅(qū)動(dòng)程序的jar檔。 

          可以用下面這段程式簡(jiǎn)單的測(cè)試一下: 
          代碼: 
            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并沒有提供連接池的功能,只能作簡(jiǎn)單的連接測(cè)試,現(xiàn)在假設(shè)連接測(cè)試沒有問題了,您想要換上DBCP以獲得連接池的功能,則原程序不用更動(dòng),只要改改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> 


              現(xiàn)在我們使用的是org.apache.commons.dbcp.BasicDataSource作為注入的DataSource源,為了使用 DBCP的功能,您必須要將commons-dbcp.jar加入CLASSPATH中,另外您還需要commons-pool.jar與commons -collections.jar,這些都可以在Spring的相依版本中的lib目錄下找到。 
              注意到我們?cè)赿ataSource Bean上宣告了destroy-method,如此可以確保BeanFactory在關(guān)閉進(jìn)也一并關(guān)閉BasicDataSource。 

              如果您的Servlet容器提供了JNDI資料源,您也可以簡(jiǎn)單的換上這個(gè)資料源: 
          代碼: 
          <?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這個(gè)類庫(kù),jndiName實(shí)際上要根據(jù)您所設(shè)定的JNDI查詢名稱,您可以在下面這個(gè)網(wǎng)址找到有關(guān)于Tomcat中JNDI設(shè)定 的方式: 
          http://www.caterpillar.onlyfun.net/phpBB2/viewtopic.php?t=1354
          posted on 2008-04-02 10:37 elite 閱讀(1242) 評(píng)論(1)  編輯  收藏 所屬分類: spring

          評(píng)論:
          # dd[未登錄] 2009-04-16 12:46 | dd

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 山东省| 板桥市| 常州市| 靖江市| 崇文区| 天柱县| 全南县| 商南县| 木里| 丽水市| 涟源市| 会理县| 宜宾县| 沅陵县| 五华县| 昌乐县| 温州市| 保定市| 栾城县| 景泰县| 稻城县| 淮北市| 密云县| 英山县| 曲周县| 五家渠市| 靖州| 仲巴县| 清丰县| 聊城市| 大兴区| 太和县| 东兰县| 抚宁县| 东至县| 樟树市| 安徽省| 萍乡市| 北海市| 安岳县| 凯里市|