领先一步
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"/>