简体   繁体   中英

Aspect on super interface method implemented in abstract super class

I have a problem quite similar to: How to create an aspect on an Interface Method that extends from A "Super" Interface , but my save method is in an abstract super class.

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 inspect any calls made to the ServiceInterface.save method.

The pointcut I have currently looks like this:

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

It gets triggered when the save method is put into the ServiceImpl , but not when it is in the SuperServiceImpl . What am I missing in my around pointcut?

I just want to pointcut on the ServiceInterface , if I do it on SuperServiceInterface wouldn't it also intercept save calls on interfaces that also inherit from SuperServiceInterface ?

Yes, but you can avoid that by restricting the target() type 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();
}

From the spring docs examples:

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

In your case it should work as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM