首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2EE

OSGi 與 Spring DM 實戰:使用 Spring DataSource 存取資料庫

     編輯:J2EE

本文為《你好,OSGi》系列第九部分。前文已完成使用 Spring DM 建構簡單應用並匯入匯出服務 Bundle 的流程,本篇將介紹 Spring DataSource 的應用,實現 Spring 應用與資料庫的連接。

Spring DataSource 核心優勢

Spring 框架強項在於提供一組預製 Bean 處理常見需求。本節將示範 Spring DM 如何讓 OSGi 應用存取這些預製 Bean。實作將修改 ContactDAO Bundle,使其連接 RDBMS 互動聯絡人資料表,連接管理使用 Spring 的 DriverManagerDataSource(屬 org.springframework.bundle.spring.jdbc 服務 Bundle)。範例採用 Apache Derby 測試,可自行替換偏好資料庫。

建立聯絡人資料表

先在資料庫建立 CONTACT 表,DDL 腳本如代碼清單 11:

CREATE TABLE CONTACT(
    CONTACTID int NOT NULL,
    FIRSTNAME varchar(300),
    LASTNAME varchar(300)
);

解決 JDBC 驅動程式相依性

DriverManagerDataSource 屬 spring.jdbc Bundle,需存取 JDBC 驅動類別。最簡解法是將 JDBC 驅動 JAR 匯入該 Bundle:

  1. 右鍵 org.springframework.bundle.spring.jdbc Bundle,選 New > Folder 命名為 "JARs folder"
  2. 右鍵該資料夾選 Import > General > File System,選擇驅動程式目錄(如 C:\software\db-derby-10.2.2.0-bin\lib),匯入 derbyclient.jar
  3. 開啟 Bundle 的 MANIFEST.MF,切換到 Runtime 標籤,將 jars/derbyclient.jar 加入 Classpath(圖 3)

將 derbyclient.jar 加入 Bundle Classpath

圖 3. 將 derbyclient.jar 加入 Bundle Classpath

(也可直接更新 Bundle-ClassPath: spring-jdbc-2.5.jar, jars/derbyclient.jar)

修改 ContactDAOImpl 實作資料存取

更新 ContactDAOImpl 類別與 CONTACT 表互動(代碼清單 12):

public class ContactDAOImpl implements ContactDAO {
    DataSource dataSource;
    public DataSource getDataSource() { return dataSource; }
    public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; }
    public List getContactList() {
        List contactList = new ArrayList();
        try {
            Connection connection = dataSource.getConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM CONTACT");
            while(rs.next()){
                int contactId = rs.getInt("CONTACTID");
                String firstName = rs.getString("FIRSTNAME");
                String lastName = rs.getString("LASTNAME");
                Contact contact = new Contact(contactId, firstName, lastName);
                contactList.add(contact);
            }
        } catch (SQLException e) { e.printStackTrace(); }
        return contactList;
    }
}

主要變更:新增 dataSource 作為 Java Bean 屬性含 getter/setter,Spring 將負責建立與注入;getContactList() 從資料源取得連線查詢聯絡人記錄。

更新 Spring 設定檔

修改 contactdao-service.xml 宣告資料源並注入服務(代碼清單 13):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="contactDAOService" class="com.javaworld.sample.osgi.spring.contact.impl.ContactDAOImpl">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
        <property name="url" value="jdbc:derby://localhost:1527/C:/derby_home/databases/ClientDB"/>
        <property name="username" value="test"/>
        <property name="password" value="test"/>
    </bean>
</beans>

修改重點:宣告 dataSource Bean 指向 DriverManagerDataSource 配置資料庫連線管理;修改 contactDAOService 定義添加對 dataSource Bean 的相依。

總結

OSGi 規範為 Spring 應用引入模組化與動態服務。對熟悉 Spring 的開發者,Spring DM 是學習 OSGi 動態模組化開發的捷徑,無需深研 OSGi API。如本文所示,Spring DM 為 OSGi 面向服務應用開發提供絕佳平台:運行系統中應用模組可動態增刪更新,並可同時部署同模組多版本。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved