Spring @ServiceActivator Integration Using Java 8 Default Interface Method

I would like to use the @ServiceActivator annotation for the default Java 8 interface method. This method will by default delegate another method of this interface depending on business rules.

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

This interface is then implemented by the Spring @Service class:

 @Service public class MyService implements MyServiceInterface { public void myMethod(...) { ... } } 

When executing the code, this does not work!

I can get it working to remove the @ServiceActivator annotation from the default method and override this default method in my @Service class and delegate the super method:

 @Service public class MyWorkingService implements MyServiceInterface { @ServiceActivator @Override public void onMessageReceived(MyPayload payload) { MyServiceInterface.super.onMessageReceived(payload); } public void myMethod(...) { ... } } 

Overriding the default method ignores the default method assignment.

Is there any other way to implement this scenario in its purest form?

+6
source share
1 answer

This does not work now because Spring Integration is based on ReflectionUtils.doWithMethods , which uses ReflectionUtils.getDeclaredMethods , and the latter only does this clazz.getDeclaredMethods() , which does not return these default methods on the interface.

Feel free to raise the JIRA issue against the Spring Framework to consider this option.

At the same time, correctly, there is no other choice but to override this method.

+2
source

Source: https://habr.com/ru/post/983867/


All Articles