6

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

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(...) {
        ...
    }
}

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

是否有另一种方法可以以干净的方式实现此场景?

4

1 回答 1

2

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

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

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

于 2015-03-16T13:19:13.810 回答