Spring Integration 2.2 于2012年发布,是该框架早期的重要版本之一
Spring Integration 2.2 于2012年发布是该框架早期的重要版本之一。其中Part 3: JPA SupportJPA 支持是该版本新增的关键特性之一标志着 Spring Integration 正式引入对 Java Persistence APIJPA的原生集成能力使消息驱动架构能更便捷地与关系型数据库交互。✅ 主要新特性JPA Support包括JPA Outbound Channel Adapter允许将消息负载如实体对象或 Map持久化到 JPA 管理的数据库中。支持自动调用EntityManager.persist()或merge()并可配置 flush 模式、事务边界等。JPA Inbound Channel Adapter周期性执行 JPQL 查询或命名查询将查询结果List作为消息发送至下游通道。支持分页、参数绑定如:timestamp、以及基于上次执行结果的增量轮询例如max(lastModified)。JPA Stored Procedure Outbound Adapter实验性支持调用 JPA 容器管理的存储过程需 JPA 2.1但 2.2 版本中为初步支持功能较基础。与 Spring Transaction 紧密集成所有 JPA 组件默认参与当前事务如Transactional方法内确保消息处理与数据库操作的原子性。配置方式友好XML Java DSL 预备支持提供int-jpa:...命名空间需引入spring-integration-jpa模块及对应 XSD例如int-jpa:inbound-channel-adapterchanneljpaChannelentity-manager-factoryemfjpa-querySELECT e FROM Employee e WHERE e.status ACTIVEupdateUPDATE Employee e SET e.processed true WHERE e.id IN :ids/⚠️ 注意Spring Integration 2.2 的 JPA 模块是轻量级封装不替代 Spring Data JPA它面向“消息流中的数据存取”场景而非领域层抽象。后续版本如 4.0逐步增强并与 Spring Data 更深度整合。!-- 示例JPA outbound adapter 持久化消息载荷 --int-jpa:outbound-channel-adapterchanneltoDatabaseChannelentity-manager-factoryentityManagerFactorypersist-modePERSIST/What New in Spring Integration 2.2 (Part 3 JPA Support)This is the third part in a series of blog posts highlighting some of the new features available in Spring Integration 2.2 following the recent release of Release Candidate 1. The first part described the new set of MongoDB adapters. In part two we highlighted the new extended support for synchronizing non-transactional resources with transactions.In this third part today, we would like to introduce the new Java Persistence API (JPA) support that is provided starting with Spring Integration 2.2. The JPA module is persistence-provider-agnostic and has been tested using:Hibernate OpenJPA EclipseLinkAs part of the new JPA module, we provide several components for retrieving and persisting JPA entity objects:JPA Inbound Channel Adapter JPA Outbound Channel Adapter JPA Updating Outbound Gateway JPA Retrieving Outbound GatewayUsing these components, you can select, create, update and delete entities in your database. Besides persisting data using the entity classes directly, you can also execute queries using the Java Persistence Query Language (JPQL) as well as using native SQL queries. Additionally, named queries are supported as well.The JPA SampleIn our Spring Integration Samples repository, we provide a sample application demonstrating the JPA support, which we want to use in this blog post to show you how to easily get started.The provided sample is using an embedded H2 database which contains a single table called PEOPLE. This table is mapped to the Person entity class in package org.springframework.integration.samples.jpa. With that setup we cover two simple use-cases:List all people from the database Create a new Person record in the databaseThe corresponding Spring Integration flow is quite simple as well. The flow is started via a Messaging Gateway. This allows us to hide the Spring Integration messaging API and to only expose a plain Java interface (PersonService) to the sample’s Main class (org.springframework.integration.samples.jpa.Main). Depending on which method is invoked on the PersonService, the Spring Integration message will be routed to either a JPA Retrieving Outbound Gateway (List all people) or a JPA Updating Outbound Gateway (Create a new Person).Execute the SampleIn order to set the sample up, please checkout the Spring Integration Samples repository using Git:$ git clone https://github.com/SpringSource/spring-integration-samples.gitNext, go to the JPA sample directory:$ cd spring-integration-samples/basic/jpaNow we can build and run the application by executing the following Maven command:$ mvn clean package exec:execEventually the application starts up and you should see the following screen: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 Hibernate2. Use OpenJPA3. Use EclipseLinkq. Quit the applicationEnter you choice:The JPA sample allows you to execute the JPA operations using one of the following persistance providers: Hibernate, OpenJPA or EclipseLink. At application startup you will therefore be able to choose the desired persistence provider. Once selected, you can select which specific JPA operation to execute:Please enter a choice and press enter:1. List all people2. Create a new personq. Quit the applicationEnter you choice:You can either list each Person from the PEOPLE table (Option 1):Enter you choice: 1ID NAME CREATED1001, Cartman, 2012-10-04 16:14:02or you can create a new Person (Option 2):Enter the Person’s name:Demo UserCreated person record with id: 1002Do you want to create another person? (y/n)…Configuration DetailsThe JPA sample is configured using several Spring XML Application Context files. For the most part, Spring Integration’s JPA support uses the JPA support provided by the core Spring Framework. Thus, the common JPA configuration is located at:/src/main/resources/META-INF/spring/integration/commonJpa-context.xmlThis file does not contain anything Spring Integration specific. All we do, is to setup the embedded database, the respective DataSource, the EntityManagerFactory and the Transaction Manager.The JPA persistence provider specific configuration is located under:/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.xmlAs you will see, these configurations are very light-weight, containing only the persistence provider specific JpaVendorAdapter bean declarations.Everything Spring Integration specific is configured in:/src/main/resources/META-INF/spring/integration/spring-integration-context.xml?xml version1.0 encodingUTF-8?int:channel idcreatePersonRequestChannel/ int:channel idlistPeopleRequestChannel/ int:gateway idpersonService service-interfaceorg.springframework.integration.samples.jpa.service.PersonService default-request-timeout5000 default-reply-timeout5000 int:method namecreatePerson request-channelcreatePersonRequestChannel/ int:method namefindPeople request-channellistPeopleRequestChannel/ /int:gateway int-jpa:retrieving-outbound-gateway entity-manager-factoryentityManagerFactory request-channellistPeopleRequestChannel jpa-queryselect p from Person p order by p.name asc /int-jpa:retrieving-outbound-gateway int-jpa:updating-outbound-gateway entity-manager-factoryentityManagerFactory request-channelcreatePersonRequestChannel int-jpa:transactional transaction-managertransactionManager / /int-jpa:updating-outbound-gateway !-- Depending on the selected profile, users can use different JPA Providers -- beans profiledefault, hibernate import resourceclasspath:/META-INF/spring/integration/hibernate-context.xml/ /beans beans profileopenjpa import resourceclasspath:/META-INF/spring/integration/openjpa-context.xml/ /beans beans profileeclipselink import resourceclasspath:/META-INF/spring/integration/eclipselink-context.xml/ /beansBoth, the Retrieving Outbound Gateway and the Updating Outbound Gateway are wired up with an EntityManagerFactory reference. Alternatively, we also provide means to pass-in a reference to an EntityManager directly.The Retrieving Outbound Gateway is also configured with an JPQL query to retrieve all people from the database ordered by their name. The Updating Outbound Gateway on the other hand does not specify any queries at all. It directly uses the Person object that is being passed in as the Spring Integration Message payload. Ultimately, the Person object is passed to the EntityManager and persisted to the database. Furthermore, the Updating Outbound Gateway is being declared transactional ensuring that the JPA session is flushed and the data committed to the database.Providing ParametersNot shown in the example above but with the JPA supporting components you are also able to provide parameters for your JPQL/SQL queries. In order to do so, you can use theint-jpa:parameter/sub-element. For example, you can provide Named Parameters by specifying:int-jpa:parameter name“myNamedParam” type“java.lang.String” value“myParamValue”/Alternatively, you can also provide Positional Parameters by leaving off the name attribute:int-jpa:parameter type“java.lang.String” value“myFirstParam”/int-jpa:parameter type“java.lang.Integer” value“2”/Lastly, if you are using the Outbound Channel Adapter or any of the Outbound Gateways, you can also provide dynamic parameters using the Spring Expression Language (SpEL), giving you easy access to values from the Message’s payload or its Message headers:int-jpa:parameter expression“payload.name” name“firstName”/ConclusionWe hope that this blog post provides you with an useful overview of the new Spring Integration JPA support. For much more detailed information, please consult the chapter titled JPA Support in the Spring Integration reference manual. Lastly, if you run into any trouble or have additional questions, please feel free to post those to our Spring Integration Forum.ResourcesSpring Integration Reference Documentation Spring Integration JPA Sample Spring Integration Forum JSR 317: Java™ Persistence 2.0