Spring Integration 2.2 的新特性(第三部分 – JPA 支援)

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

這是系列部落格文章中的第三部分,重點介紹 Spring Integration 2.2 中的一些新特性,該版本緊隨最近釋出的 Release Candidate 1第一部分介紹了新的 MongoDB 介面卡。 在第二部分中,我們重點介紹了對非事務性資源與事務同步的新擴充套件支援。

今天的這第三部分,我們 將介紹從 Spring Integration 2.2 開始提供的新 Java Persistence 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 流程也非常簡單。該流程透過 Messaging Gateway 啟動。這使得我們可以隱藏 Spring Integration 訊息 API,並僅向示例的 Main 類 (org.springframework.integration.samples.jpa.Main) 暴露一個普通 Java 介面 (PersonService)。根據在 PersonService 上呼叫的方法,Spring Integration 訊息將被路由到 JPA 檢索出站閘道器(列出所有人)或 JPA 更新出站閘道器(建立一個新的 Person)。

執行示例

為了設定示例,請使用 Git 克隆 Spring Integration Samples 倉庫

    $ 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>

Retrieving Outbound GatewayUpdating Outbound Gateway 都連線到了一個 EntityManagerFactory 引用。此外,我們也提供了直接傳入 EntityManager 引用的方式。

Retrieving Outbound Gateway 也配置了一個 JPQL 查詢,用於從資料庫中按姓名排序檢索所有人。另一方面,Updating Outbound Gateway 沒有指定任何查詢。它直接使用作為 Spring Integration 訊息 payload 傳入的 Person 物件。最終,Person 物件被傳遞給 EntityManager 並持久化到資料庫。此外,Updating Outbound Gateway 被宣告為事務性的,確保 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"/>

最後,如果您使用 Outbound Channel Adapter 或任何 Outbound Gateway,您還可以使用 Spring Expression Language (SpEL) 提供動態引數,讓您輕鬆訪問訊息的 payload 或訊息頭中的值。


<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 社群的所有近期活動。

檢視全部