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

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

这是系列博客文章的第三部分,重点介绍了在最近发布 Release Candidate 1 之后 Spring Integration 2.2 中可用的一些新功能。第一部分描述了新的一组 MongoDB 适配器。在第二部分中,我们重点介绍了对事务同步非事务性资源的新扩展支持。

在今天的第三部分中,我们想介绍从 Spring Integration 2.2 开始提供的新的 Java Persistence API (JPA) 支持。JPA 模块是与持久化提供程序无关的,并已使用以下方式进行测试:

作为新 JPA 模块的一部分,我们提供了几个用于检索和持久化 JPA 实体对象的组件
  • JPA 入站通道适配器
  • JPA 出站通道适配器
  • JPA 更新出站网关
  • JPA 检索出站网关
使用这些组件,您可以在数据库中选择、创建、更新和删除实体。除了直接使用实体类持久化数据外,您还可以使用 Java Persistence Query Language (JPQL) 以及原生 SQL 查询来执行查询。此外,还支持命名查询。

JPA 示例

在我们的 Spring Integration 示例 仓库中,我们提供了 一个示例应用程序,演示了 JPA 支持,我们将在本博客文章中使用它来向您展示如何轻松入门。

所提供的示例使用了嵌入式 H2 数据库,其中包含一个名为 PEOPLE 的表。此表映射到 org.springframework.integration.samples.jpa 包中的 Person 实体类。通过此设置,我们涵盖了两个简单的用例:

  • 从数据库中列出所有人员
  • 在数据库中创建新的 Person 记录
相应的 Spring Integration 流程也相当简单。该流程通过 消息网关 启动。这允许我们隐藏 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 和事务管理器。

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 会话被刷新并且数据被提交到数据库。

提供参数

虽然上面示例中没有展示,但是使用 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"/>

最后,如果您正在使用出站通道适配器或任何出站网关,您还可以使用 Spring 表达式语言 (SpEL) 提供动态参数,让您轻松访问消息有效负载或其消息头中的值


<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 社区所有即将举行的活动。

查看所有