什么是最简单的切入点表达式,它可以拦截所有用 @Service
注释的 bean 的所有公共(public)方法?例如,我希望它会影响这个 bean 的两个公共(public)方法:
@Service
public MyServiceImpl implements MyService {
public String doThis() {...}
public int doThat() {...}
protected int doThatHelper() {...} // not wrapped
}
最佳答案
这documentation应该非常有帮助。
我会创建两个单独的切入点,一个用于所有公共(public)方法,一个用于所有用@Service 注释的类,然后创建第三个结合其他两个切入点表达式的切入点。
查看(7.2.3.1 支持的切入点指示符)要使用哪些指示符。我认为您正在寻找用于查找公共(public)方法的“执行”指示符,以及用于查找注释的“注释”指示符。
然后看一下(7.2.3.2 Combining pointcut expressions)来组合它们。
我在下面提供了一些代码(我没有测试过)。它主要取自文档。
@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() {}
关于java - 服务的任何公共(public)方法的 AOP 切入点表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6861016/