任何公共服务方法的AOP切入点表达式

Kon*_*rus 5 java aop spring aspectj spring-aop

什么是最简单的切入点表达式,它将拦截所有使用注释的bean的所有公共方法@Service?例如,我希望它会影响这个bean的两个公共方法:

@Service
public MyServiceImpl implements MyService {
    public String doThis() {...}
    public int doThat() {...}
    protected int doThatHelper() {...} // not wrapped
}
Run Code Online (Sandbox Code Playgroud)

nic*_*ild 4

文档应该非常有帮助。

我将通过创建两个单独的切入点,一个用于所有公共方法,一个用于所有用 @Service 注释的类,然后创建第三个切入点,它结合了其他两个切入点表达式。

查看(7.2.3.1 支持的切入点指示符)要使用哪些指示符。我认为您正在寻找用于查找公共方法的“执行”指示符,以及用于查找注释的“注释”指示符。

然后看一下(7.2.3.2 组合切入点表达式)来组合它们。

我在下面提供了一些代码(我尚未测试)。它主要取自文档。

@Pointcut("execution(public * *(..))") //this should work for the public pointcut
private void anyPublicOperation() {}

//@Pointcut("@annotation(Service)") this might still work, but try 'within' instead
@Pointcut("@within(Service)") //this should work for the annotation service pointcut
private void inTrading() {}

@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}
Run Code Online (Sandbox Code Playgroud)