Spring Integration 2.2 新功能 (第 3 部分 – JPA 支援)

工程 | Gunnar Hillert | 2012 年 10 月 05 日 | ...

這是部落格文章系列中的第三部分,重點介紹在最近發布 Release Candidate 1 後,Spring Integration 2.2 中提供的一些新功能。第一部分 描述了新的 MongoDB 适配器集。在第二部分 中,我們重點介紹了對非交易資源與交易同步的新增擴展支援。

在今天的第三部分中,我們想要介紹從 Spring Integration 2.2 開始提供的新 Java 持久化 API (JPA) 支援。JPA 模組與持久化供應商無關,並已使用以下項目進行測試:

作為新 JPA 模組的一部分,我們提供了多個元件,用於檢索和持久化 JPA 實體物件
  • JPA 輸入通道适配器
  • JPA 輸出通道适配器
  • JPA 更新輸出閘道器
  • JPA 檢索輸出閘道器
使用這些元件,您可以選擇、建立、更新和刪除資料庫中的實體。除了直接使用實體類別持久化資料外,您還可以執行使用 Java 持久化查詢語言 (JPQL) 以及使用原生 SQL 查詢的查詢。此外,也支援具名查詢。

JPA 範例

在我們的 Spring Integration 範例 儲存庫中,我們提供了 範例應用程式,示範 JPA 支援,我們想在本部落格文章中使用它來向您展示如何輕鬆入門。

提供的範例使用嵌入式 H2 資料庫,其中包含一個名為 PEOPLE 的單一表格。此表格對應到 org.springframework.integration.samples.jpa 套件中的 Person 實體類別。透過此設定,我們涵蓋了兩個簡單的使用案例

  • 列出資料庫中的所有人
  • 在資料庫中建立新的 Person 記錄
相應的 Spring Integration 流程也很簡單。流程透過 訊息閘道器 啟動。這讓我們可以隱藏 Spring Integration 訊息 API,並且僅向範例的 Main 類別 (org.springframework.integration.samples.jpa.Main) 公開純 Java 介面 (PersonService)。根據在 PersonService 上調用的方法,Spring Integration 訊息將被路由到 JPA 檢索輸出閘道器 (列出所有人) 或 JPA 更新輸出閘道器 (建立新的 Person)。

執行範例

為了設定範例,請使用 Git 檢出 Spring Integration 範例儲存庫

    $ git clone https://github.com/SpringSource/spring-integration-samples.git

接下來,前往 JPA 範例目錄


    $ cd spring-integration-samples/basic/jpa

現在我們可以透過執行以下 Maven 命令來建置和執行應用程式


    $ mvn clean package exec:exec

最終應用程式啟動,您應該會看到以下畫面


=========================================================

    Welcome to the Spring Integration JPA Sample!

    For more information please visit:
    http://www.springintegration.org/

=========================================================
Please enter a choice and press enter:
	1. Use Hibernate
	2. Use OpenJPA
	3. Use EclipseLink
	q. Quit the application
Enter you choice:

JPA 範例允許您使用以下持久化供應商之一執行 JPA 操作:Hibernate、OpenJPA 或 EclipseLink。因此,在應用程式啟動時,您將能夠選擇所需的持久化供應商。選擇後,您可以選擇要執行的特定 JPA 操作


Please enter a choice and press enter:
	1. List all people
	2. Create a new person
	q. Quit the application
Enter you choice:

您可以從 PEOPLE 表格中列出每個 Person (選項 1)


Enter you choice: 1
ID            NAME         CREATED
==================================
1001, Cartman, 2012-10-04 16:14:02
==================================

或者您可以建立新的 Person (選項 2)


Enter the Person's name:Demo User
Created person record with id: 1002
Do you want to create another person? (y/n)
...

組態詳細資訊

JPA 範例是使用多個 Spring XML 應用程式上下文檔案進行組態的。在大多數情況下,Spring Integration 的 JPA 支援使用核心 Spring Framework 提供的 JPA 支援。因此,常見的 JPA 組態位於

/src/main/resources/META-INF/spring/integration/commonJpa-context.xml

此檔案不包含任何 Spring Integration 特有的內容。我們所做的只是設定嵌入式資料庫、各自的 DataSource、EntityManagerFactory 和 Transaction Manager。

JPA 持久化供應商特定的組態位於


/src/main/resources/META-INF/spring/integration/eclipselink-context.xml
/src/main/resources/META-INF/spring/integration/hibernate-context.xml
/src/main/resources/META-INF/spring/integration/openjpa-context.xml

您將看到,這些組態非常輕量級,僅包含持久化供應商特定的 JpaVendorAdapter Bean 宣告。

所有 Spring Integration 特有的內容都在以下位置組態


/src/main/resources/META-INF/spring/integration/spring-integration-context.xml

<?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:int="http://www.springframework.org/schema/integration"
	xmlns:int-jpa="http://www.springframework.org/schema/integration/jpa"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/integration/jpa http://www.springframework.org/schema/integration/jpa/spring-integration-jpa.xsd">

	<int:channel id="createPersonRequestChannel"/>
	<int:channel id="listPeopleRequestChannel"/>

	<int:gateway id="personService"
		service-interface="org.springframework.integration.samples.jpa.service.PersonService"
		default-request-timeout="5000" default-reply-timeout="5000">
		<int:method name="createPerson" request-channel="createPersonRequestChannel"/>
		<int:method name="findPeople"   request-channel="listPeopleRequestChannel"/>
	</int:gateway>

	<int-jpa:retrieving-outbound-gateway entity-manager-factory="entityManagerFactory"
		request-channel="listPeopleRequestChannel"
		jpa-query="select p from Person p order by p.name asc">
	</int-jpa:retrieving-outbound-gateway>

	<int-jpa:updating-outbound-gateway entity-manager-factory="entityManagerFactory"
		request-channel="createPersonRequestChannel" >
		<int-jpa:transactional transaction-manager="transactionManager" />
	</int-jpa:updating-outbound-gateway>

	<!-- Depending on the selected profile, users can use different JPA Providers -->

	<beans profile="default, hibernate">
		<import resource="classpath:/META-INF/spring/integration/hibernate-context.xml"/>
	</beans>
	<beans profile="openjpa">
		<import resource="classpath:/META-INF/spring/integration/openjpa-context.xml"/>
	</beans>
	<beans profile="eclipselink">
		<import resource="classpath:/META-INF/spring/integration/eclipselink-context.xml"/>
	</beans>
</beans>

檢索輸出閘道器更新輸出閘道器 都與 EntityManagerFactory 參考連結。或者,我們也提供方法直接傳入對 EntityManager 的參考。

檢索輸出閘道器 也使用 JPQL 查詢進行組態,以檢索資料庫中按名稱排序的所有人員。另一方面,更新輸出閘道器 完全沒有指定任何查詢。它直接使用作為 Spring Integration Message 酬載傳入的 Person 物件。最終,Person 物件會傳遞到 EntityManager 並持久化到資料庫。此外,更新輸出閘道器 被宣告為交易性的,確保 JPA 會話被刷新,並且資料被提交到資料庫。

提供參數

在上面的範例中未顯示,但透過 JPA 支援元件,您也可以為 JPQL/SQL 查詢提供參數。為了做到這一點,您可以使用

<int-jpa:parameter/>

子元素。例如,您可以透過指定以下內容來提供具名參數


<int-jpa:parameter name="myNamedParam" type="java.lang.String" value="myParamValue"/>

或者,您也可以透過省略 name 屬性來提供位置參數


<int-jpa:parameter type="java.lang.String" value="myFirstParam"/>
<int-jpa:parameter type="java.lang.Integer" value="2"/>

最後,如果您使用的是輸出通道适配器 或任何輸出閘道器,您也可以使用 Spring 運算式語言 (SpEL) 提供動態參數,讓您可以輕鬆存取訊息酬載或其訊息標頭中的值


<int-jpa:parameter expression="payload.name" name="firstName"/>

結論

我們希望這篇部落格文章為您提供 Spring Integration JPA 支援的實用概述。如需更詳細的資訊,請查閱 Spring Integration 參考手冊中標題為 JPA 支援 的章節。最後,如果您遇到任何問題或有其他問題,請隨時將這些問題發佈到我們的 Spring Integration 論壇

資源

取得 Spring 電子報

保持與 Spring 電子報的聯繫

訂閱

領先一步

VMware 提供培訓和認證,以加速您的進展。

了解更多

取得支援

Tanzu Spring 在一個簡單的訂閱中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位檔案。

了解更多

即將到來的活動

查看 Spring 社群中所有即將到來的活動。

查看全部