Super Interface Aspect Implemented in an Abstract Superclass

I have a problem very similar to: How to create an aspect in an interface method that extends from A "Super" Interface , but my save method is in an abstract superclass.

The structure is as follows:

Interfaces:

public interface SuperServiceInterface { ReturnObj save(ParamObj); } public interface ServiceInterface extends SuperServiceInterface { ... } 

Implementations:

 public abstract class SuperServiceImpl implements SuperServiceInterface { public ReturnObj save(ParamObj) { ... } } public class ServiceImpl implements ServiceInterface extends SuperServiceImpl { ... } 

I want to check for any calls made using the ServiceInterface.save method.

The pointcut point currently looks like this:

 @Around("within(com.xyz.api.ServiceInterface+) && execution(* save(..))") public Object pointCut(final ProceedingJoinPoint call) throws Throwable { } 

It starts when the save method is placed in ServiceImpl , but not when it is in SuperServiceImpl . What am I missing in my pointcut circle?

+1
source share
2 answers

I just want to point to ServiceInterface , if I do it on SuperServiceInterface , will it not intercept save calls on interfaces that also inherit from SuperServiceInterface ?

Yes, but you can avoid this by restricting the type of target() to ServiceInterface as follows:

 @Around("execution(* save(..)) && target(serviceInterface)") public Object pointCut(ProceedingJoinPoint thisJoinPoint, ServiceInterface serviceInterface) throws Throwable { System.out.println(thisJoinPoint); return thisJoinPoint.proceed(); } 
0
source

In the spring docs examples:

execution of any method defined by the AccountService interface:
execution(* com.xyz.service.AccountService.*(..))

In your case, this should work as follows:

execution(* com.xyz.service.SuperServiceInterface.save(..))

0
source

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


All Articles