Aspect on super interface method implemented in abstract super class

冷暖自知 提交于 2019-12-13 02:59:44

问题


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?


回答1:


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();
}



回答2:


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



来源:https://stackoverflow.com/questions/28106880/aspect-on-super-interface-method-implemented-in-abstract-super-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!