【问题标题】:Spring Integration @ServiceActivator on a Java 8 default interface methodJava 8默认接口方法上的Spring集成@ServiceActivator
【发布时间】:2015-05-18 12:35:59
【问题描述】:

我想在 Java 8 默认接口方法上使用 @ServiceActivator 注释。这个默认方法会根据业务规则委托给这个接口的另一个方法。

public interface MyServiceInterface {

    @ServiceActivator
    public default void onMessageReceived(MyPayload payload) {
        if(payload.getAction() == MyServiceAction.MY_METHOD) {
            ...
            myMethod(...);
        }
    }

    public void myMethod(...);
}

这个接口然后由 Spring @Service 类实现:

@Service
public class MyService implements MyServiceInterface {

    public void myMethod(...) {
        ...
    }
}

在执行代码时,这不起作用!

我只能让它从默认方法中删除 @ServiceActivator 注释,并在我的 @Service 类中覆盖该默认方法并委托给超级方法:

@Service
public class MyWorkingService implements MyServiceInterface {

    @ServiceActivator
    @Override
    public void onMessageReceived(MyPayload payload) {
        MyServiceInterface.super.onMessageReceived(payload);
    }

    public void myMethod(...) {
        ...
    }
}

覆盖默认方法会忽略默认方法的用途。

有没有其他方法可以干净利落地实现这个场景?

【问题讨论】:

    标签: java spring java-8 spring-integration default-method


    【解决方案1】:

    这现在不起作用,因为 Spring Integration 依赖于 ReflectionUtils.doWithMethods,它使用 ReflectionUtils.getDeclaredMethods,而最后一个只是这样做 clazz.getDeclaredMethods(),它不会在接口上返回那些 default 方法。

    请随时针对 Spring Framework 提出 JIRA 问题以考虑该选项。

    与此同时,没错,除非重写该方法,否则别无选择。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2019-04-02
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多