我想@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(...) {
...
}
}
覆盖默认方法会忽略默认方法的用途。
是否有另一种方法可以以干净的方式实现此场景?