Java 8默认接口方法上的Spring Integration @ServiceActivator

Ste*_*yts 6 java spring spring-integration java-8 default-method

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

public interface MyServiceInterface {

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

    public void myMethod(...);
}
Run Code Online (Sandbox Code Playgroud)

然后,此接口由Spring @Service类实现:

@Service
public class MyService implements MyServiceInterface {

    public void myMethod(...) {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

@Service
public class MyWorkingService implements MyServiceInterface {

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

    public void myMethod(...) {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

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

是否有其他方式以干净的方式实现此方案?

Art*_*lan 2

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

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

同时,对,除了重写该方法之外别无选择。