Namespace Support

使用 XML 名称空间支持时,底层解析器类可为您实例化相关的 Java 类。因此,通常无需处理 JPA 适配器的内部工作原理。本节记录了 Spring Integration 提供的 XML 名称空间支持,并向您展示如何使用 XML 名称空间支持来配置 JPA 组件。

Common XML Namespace Configuration Attributes

所有 JPA 组件都共享某些配置参数:

auto-startup

Lifecycle attribute that signals whether this component should be started during application context startup. Defaults to true. Optional.

id

Identifies the underlying Spring bean definition, which is an instance of either EventDrivenConsumer or PollingConsumer. Optional.

entity-manager-factory

The reference to the JPA entity manager factory that the adapter uses to create the EntityManager. You must provide this attribute, the entity-manager attribute, or the jpa-operations attribute.

entity-manager

The reference to the JPA Entity Manager that the component uses. You must provide this attribute, the entity-manager-factory attribute, or the jpa-operations attribute.

通常,你的 Spring 应用程序上下文仅定义一个 JPA 实体管理器工厂,并且 EntityManager 通过使用 @PersistenceContext 注解进行注入。此方法不适用于 Spring Integration JPA 组件。通常,最好注入 JPA 实体管理器工厂,但是,当你想显式注入 EntityManager 时,你必须定义一个 SharedEntityManagerBean。有关详细信息,请参阅相关的 Javadoc

以下示例展示如何显式包含一个实体管理器工厂:

<bean id="entityManager"
      class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
jpa-operations

A reference to a bean that implements the JpaOperations interface. In rare cases, it might be advisable to provide your own implementation of the JpaOperations interface instead of relying on the default implementation (org.springframework.integration.jpa.core.DefaultJpaOperations). If you use the jpa-operations attribute, you must not provide the JPA entity manager or JPA entity manager factory, because JpaOperations wraps the necessary datasource.

entity-class

The fully qualified name of the entity class. The exact semantics of this attribute vary, depending on whether we are performing a persist or update operation or whether we are retrieving objects from the database.

在检索数据时,您可以指定 entity-class 属性,以表明您希望从数据库中检索此类型的对象。在这种情况下,您不得定义任何查询属性(jpa-querynative-querynamed-query)。 在持久化数据时,entity-class 属性表示要持久化的对象类型。如果没有指定(对于持久化操作),则从消息的有效负载自动检索实体类。

jpa-query

Defines the JPA query (Java Persistence Query Language) to be used.

native-query

Defines the native SQL query to be used.

named-query

Refers to a named query. A named query can be defined in either Native SQL or JPAQL, but the underlying JPA persistence provider handles that distinction internally.

Providing JPA Query Parameters

要提供参数,您可以使用 parameter XML 元素。它具有一种机制,使您可以为基于 Java 持久性查询语言 (JPQL) 或本机 SQL 查询的查询提供参数。您还可为命名查询提供参数。

Expression-based Parameters

The following example shows how to set an expression-based parameter:

<int-jpa:parameter expression="payload.name" name="firstName"/>
Value-based Parameters

The following example shows how to set a value-based parameter:

<int-jpa:parameter name="name" type="java.lang.String" value="myName"/>
Positional Parameters

The following example shows how to set an expression-based parameter:

<int-jpa:parameter expression="payload.name"/>
<int-jpa:parameter type="java.lang.Integer" value="21"/>

Transaction Handling

所有 JPA 操作(如 INSERTUPDATEDELETE)都要求在执行操作时保持事务处于活动状态。对于入站通道适配器,无需执行任何特殊操作。其工作方式类似于我们在使用其他入站通道适配器时使用轮询器配置事务管理器的方式。以下 XML 示例配置了一个使用轮询器和入站通道适配器的事务管理器:

<int-jpa:inbound-channel-adapter
    channel="inboundChannelAdapterOne"
    entity-manager="em"
    auto-startup="true"
    jpa-query="select s from Student s"
    expect-single-result="true"
    delete-after-poll="true">
    <int:poller fixed-rate="2000" >
        <int:transactional propagation="REQUIRED"
            transaction-manager="transactionManager"/>
    </int:poller>
</int-jpa:inbound-channel-adapter>

但是,在使用出站通道适配器或网关时,可能需要专门启动事务。如果 DirectChannel 是出站适配器或网关的输入通道,并且事务在当前执行线程中处于活动状态,则 JPA 操作在相同的事务上下文中执行。您还可以配置此 JPA 操作以作为新事务运行,如下例所示:

<int-jpa:outbound-gateway
    request-channel="namedQueryRequestChannel"
    reply-channel="namedQueryResponseChannel"
    named-query="updateStudentByRollNumber"
    entity-manager="em"
    gateway-type="UPDATING">
    <int-jpa:parameter name="lastName" expression="payload"/>
    <int-jpa:parameter name="rollNumber" expression="headers['rollNumber']"/>
		<int-jpa:transactional propagation="REQUIRES_NEW"
        transaction-manager="transactionManager"/>
</int-jpa:outbound-gateway>

在前述示例中,出站网关或适配器的 transactional 元素指定了事务属性。如果在适配器中将 DirectChannel 作为输入通道,并且希望适配器在与调用者相同的事务上下文中执行操作,那么定义此子元素是可选的。但是,如果您使用 ExecutorChannel,则必须有 transactional 元素,因为未传播调用客户端的事务上下文。

与在 Spring Integration 名称空间中定义的轮询器的 transactional 元素不同,出站网关或适配器的 transactional 元素在 JPA 名称空间中定义。