【问题标题】:Spring Integration service-activator referencing Hibernate JPA repository methodSpring Integration service-activator 引用 Hibernate JPA 存储库方法
【发布时间】:2018-04-29 09:14:31
【问题描述】:

我正在尝试使用 Hibernate JPA 存储库方法作为服务激活器的方法。链中的定义如下所示。

<int:chain input-channel="bootstrapLineupCdsStart" output-channel="bootstrapLineupItemCdsStart">
    <int:service-activator ref="lineupRepository" method="findByIntegrationStatusNew" />
    <int:filter expression="!payload.isEmpty()" discard-channel="bootstrapLineupItemCdsStart"/>
    <int:splitter expression="payload"/>
    <int:service-activator ref="lineupServicePipeline" method="createLineup"/>
    <int:aggregator/>
</int:chain>

违规行是第一个服务激活器,特别是这一行&lt;int:service-activator ref="lineupRepository" method="findByIntegrationStatusNew" /&gt;

为了完整起见,这里是存储库类本身

@Repository( "lineupRepository" )
public interface LineupRepository extends JpaRepository<LineupEntity, Integer> {
    @Query("select l from LineupEntity l where l.integrationStatus = 0")
    List<LineupEntity> findByIntegrationStatusNew();
}

现在由于 spring/hibernate 的魔力,我自己并没有真正实现 findByIntegrationStatusNew;这是由框架在运行时动态实现的。我也不需要显式定义 lineupRepository bean,因为 @Repository 注释会为我处理这个问题。

由于上述原因,我得到以下异常。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.handler.MessageHandlerChain#9$child#0.handler': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Target object of type [class com.sun.proxy.$Proxy79] has no eligible methods for handling Messages.

最后,我确实有一个似乎可以完成工作的工作;这是一个明显的工作,我怀疑不应该是必要的。解决方法包括创建一个新 bean、在上下文 XML 中显式定义 bean 并调用该方法而不是存储库上的方法。所以我的工作看起来有点像这样;首先,解决方法的上下文看起来像这样。

<int:chain input-channel="bootstrapLineupCdsStart" output-channel="bootstrapLineupItemCdsStart">
    <int:service-activator ref="lineupRepositoryService" method="findByIntegrationStatusNew" />
    <int:filter expression="!payload.isEmpty()" discard-channel="bootstrapLineupItemCdsStart"/>
    <int:splitter expression="payload"/>
    <int:service-activator ref="lineupServicePipeline" method="createLineup"/>
    <int:aggregator/>
</int:chain>
<bean id="lineupRepositoryService" class="com.mypackage.LineupRepositoryService"/>

注意第一个服务激活器现在引用 LineupRepositoryService 而不是 LineupRepository,我还添加了一个新的 bean 定义。

在第二个工作中,当然,我还定义了一个 LineupRespositoryService 类,如下所示。

public class LineupRepositoryService {
    @Autowired
    private LineupRepository lineupRepository;

    public List<LineupEntity> findByIntegrationStatusNew() {
        return lineupRepository.findByIntegrationStatusNew();
    }
}

解决方法有效,但我宁愿以正确的方式进行。所以有人知道我怎样才能让它正常工作或这里发生了什么?

【问题讨论】:

  • 如果您使用 CGLIB 代理而不是 JDK 代理来进行 repo,它可能会起作用。
  • @GaryRussell 我不太确定该怎么做。这是我第一次使用 spring 的存储库,我所做的只是注释它,它神奇地起作用了。如果你能提供一些关于如何做的信息,我很乐意尝试。
  • 我不完全确定; JPA 尽可能使用 JDK 代理;阅读this question 的答案,将接口更改为类并将查找器声明为抽象可能会起作用。
  • @GaryRussell 值得一试,谢谢。我怀疑它会起作用,但至少不会阻止我尝试。
  • @JeffreyPhillipsFreeman,请与我们分享您正在使用的 Spring Integration 版本

标签: java spring hibernate jpa spring-integration


【解决方案1】:

@JeffreyPhillipsFreeman,我们通过测试用例确认这是从 Spring Integration 反射方法调用的角度来看的问题。

无论如何都需要修复它:https://jira.spring.io/browse/INT-3820

虽然只有像 service wrapper 这样的解决方法。

【讨论】:

  • 只是我的运气。知道尝试早期版本是否可以解决问题吗?我可以试试。
  • 我今天将考虑这项任务,但不确定修复是否会如此容易包含在即将发布的4.2.GA 中。所以,可能会被推送到4.3...
  • 好的,请告诉我进展如何。我将尝试查看票以及此线程。非常感谢。
  • 顺便说一句,我尝试了您的拉取请求,它确实为我解决了问题,谢谢。
猜你喜欢
  • 2019-06-29
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 2017-09-16
  • 2016-12-22
  • 2017-08-20
  • 1970-01-01
  • 2018-07-26
相关资源
最近更新 更多