本文介绍了服务的任何公共方法的AOP切入点表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最简单的切入点表达式是什么,它将拦截所有用@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
}

推荐答案

文档应该会非常有帮助.

我将创建两个单独的切入点,一个用于所有公共方法,一个对所有使用@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切入点表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 17:34