保持领先
VMware 提供培训和认证,以加速您的进步。
了解更多这是系列博客文章中的第三部分,重点介绍 Spring Integration 2.2 中的一些新特性,该版本紧随最近发布的 Release Candidate 1。 第一部分介绍了新的 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 和 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 Gateway 和 Updating Outbound Gateway 都连接到了一个 EntityManagerFactory 引用。此外,我们也提供了直接传入 EntityManager 引用的方式。
Retrieving Outbound Gateway 也配置了一个 JPQL 查询,用于从数据库中按姓名排序检索所有人。另一方面,Updating Outbound Gateway 没有指定任何查询。它直接使用作为 Spring Integration 消息 payload 传入的 Person 对象。最终,Person 对象被传递给 EntityManager 并持久化到数据库。此外,Updating Outbound Gateway 被声明为事务性的,确保 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"/>
最后,如果您使用 Outbound Channel Adapter 或任何 Outbound Gateway,您还可以使用 Spring Expression Language (SpEL) 提供动态参数,让您轻松访问消息的 payload 或消息头中的值。
<int-jpa:parameter expression="payload.name" name="firstName"/>