Java 服务的任何公共方法的AOP切入点表达式
最简单的切入点表达式是什么,它可以拦截所有bean的所有公共方法,这些bean用Java 服务的任何公共方法的AOP切入点表达式,java,spring,aop,aspectj,spring-aop,Java,Spring,Aop,Aspectj,Spring Aop,最简单的切入点表达式是什么,它可以拦截所有bean的所有公共方法,这些bean用@Service注释?例如,我希望它会影响这个bean的两个公共方法: @Service public MyServiceImpl implements MyService { public String doThis() {...} public int doThat() {...} protected int doThatHelper() {...} // not wrapped } 这
@Service
注释?例如,我希望它会影响这个bean的两个公共方法:
@Service
public MyServiceImpl implements MyService {
public String doThis() {...}
public int doThat() {...}
protected int doThatHelper() {...} // not wrapped
}
这应该非常有帮助
我将创建两个单独的切入点,一个用于所有公共方法,一个用于所有用@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() {}
这些切入点是否经过测试?据我所知,
@annotation
没有定义连接点,而是收集其上下文。也许你应该在中使用?我在回答中提到我没有测试这些切入点。但是我同意,提问者可能更愿意使用@inthein来达到期望的结果。那么修改你的答案不是更好吗?我认为它可能不会像现在这样起作用。我已经用内的完成了问题所要求的,而且它是有效的,但我现在意识到我应该在内使用。我认为内的在您查看给定匹配者的“内部”时使用,而@内的在您查看带有匹配注释的“内部”时使用。另外,我可能在之前的评论中没有提到它,但我确实更新了代码。