问题描述
拦截所有用@Service
注释的bean 的所有公共方法的最简单的切入点表达式是什么?例如,我希望它影响这个 bean 的两个公共方法:
What is the simplest pointcut expression that would intercept all public methods of all beans annotated with @Service
? For instance, I expect it to affect both public methods of this bean:
@Service
public MyServiceImpl implements MyService {
public String doThis() {...}
public int doThat() {...}
protected int doThatHelper() {...} // not wrapped
}
推荐答案
这个 文档 应该非常有用.
This documentation should be extremely helpful.
我会创建两个单独的切入点,一个用于所有公共方法,一个用于所有用@Service 注释的类,然后创建第三个结合其他两个切入点表达式的切入点.
I would do by creating two individual point cuts, one for all public methods, and one for all classes annotated with @Service, and then create a third one that combines the pointcut expressions of the other two.
查看(7.2.3.1 支持的切入点指示符)要使用的指示符.我认为您在寻找公共方法的执行"指示符和用于查找注释的注释"指示符.
Take a look at (7.2.3.1 Supported Pointcut Designators) for which designators to use. I think you are after 'execution' designator for finding public methods, and the 'annotation' designator for finding your annotation.
然后看一下(7.2.3.2 组合切入点表达式)来组合它们.
Then take a look at (7.2.3.2 Combining pointcut expressions) for combining them.
我在下面提供了一些代码(我没有测试过).它主要取自文档.
I have provided some code below (which I have not tested). It is mostly taken from the documentation.
@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() {}
这篇关于服务的任何公共方法的 AOP 切入点表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!