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

查看全部