領先一步
VMware 提供培訓和認證,助您加速進步。
瞭解更多這是系列部落格文章的第三部分,重點介紹了在最近釋出 Release Candidate 1 之後 Spring Integration 2.2 中可用的一些新功能。第一部分描述了新的一組 MongoDB 介面卡。在第二部分中,我們重點介紹了對事務同步非事務性資源的新擴充套件支援。
在今天的第三部分中,我們想介紹從 Spring Integration 2.2 開始提供的新的 Java Persistence API (JPA) 支援。JPA 模組是與持久化提供程式無關的,並已使用以下方式進行測試:
作為新 JPA 模組的一部分,我們提供了幾個用於檢索和持久化 JPA 實體物件的元件所提供的示例使用了嵌入式 H2 資料庫,其中包含一個名為 PEOPLE 的表。此表對映到 org.springframework.integration.samples.jpa 包中的 Person 實體類。透過此設定,我們涵蓋了兩個簡單的用例:
$ 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)
...
/src/main/resources/META-INF/spring/integration/commonJpa-context.xml
此檔案不包含任何 Spring Integration 特有的內容。我們所做的只是設定嵌入式資料庫、相應的 DataSource、EntityManagerFactory 和事務管理器。
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 訊息 有效負載傳入的 Person 物件。最終,Person 物件被傳遞給 EntityManager 並持久化到資料庫。此外,更新出站閘道器被宣告為事務性的,確保 JPA 會話被重新整理並且資料被提交到資料庫。
<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"/>