AND、OR 和 NOT 的 Spring AOP 切入点语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2266850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:59:23  来源:igfitidea点击:

Spring AOP Pointcut syntax for AND, OR and NOT

springaopspring-aop

提问by Odinodin

I'm having trouble with a pointcut definition in Spring (version 2.5.6). I'm trying to intercept all method calls to a class, except for a given method (someMethod in the example below).

我在 Spring(2.5.6 版)中遇到了切入点定义的问题。我正在尝试拦截对类的所有方法调用,除了给定的方法(下面示例中的 someMethod )。

<aop:config>
    <aop:advisor
         pointcut="execution(* x.y.z.ClassName.*(..)) AND NOT
                   execution(* x.y.x.ClassName.someMethod(..))"
    />
</aop:config>

However, the interceptor is invoked for someMethod as well.

但是,拦截器也会为 someMethod 调用。

Then I tried this:

然后我尝试了这个:

<aop:config>
    <aop:advisor
         pointcut="execution(* x.y.z.ClassName.(* AND NOT someMethod)(..)) )"
    />
</aop:config>

But this does not compile as it is not valid syntax (I get a BeanCreationException).

但这不能编译,因为它不是有效的语法(我得到一个 BeanCreationException)。

Can anybody give any tips?

任何人都可以提供任何提示吗?

回答by Declan

i know its probably a bit late at this stage but ive been having the same issue and I resolved it by escaping the ampersand chars so its &amp;&amp; !instead of 'AND NOT' or '&& !'. I'm doing this in the xml file

我知道在这个阶段它可能有点晚了,但我遇到了同样的问题,我通过转义与符号来解决它,所以它&amp;&amp; !而不是“AND NOT”或“&&!”。我在 xml 文件中这样做

<aop:config>
    <aop:pointcut id="blah" expression="execution(* com.disney.goofy..*.*(..)) &amp;&amp; !@annotation(com.disney.goofy.NonDisneyCharacter)"/>
    <aop:advisor advice-ref="transAdvice" pointcut-ref="blah"/>
</aop:config>

This applies the advice to all methods executed in com.disney.goofy and that are not annotated with NonDisneyCharacter

这将建议应用于在 com.disney.goofy 中执行且未使用 NonDisneyCharacter 注释的所有方法

回答by crunchdog

This should work (spring AOP reference):

这应该有效(弹簧 AOP 参考):

pointcut="execution(* x.y.z.ClassName.*(..))
          && !execution(* x.y.x.ClassName.someMethod(..))"

回答by James Scriven

I'm also using spring 2.5.6 and was having a similar problem with ORnot working, but ANDwasworking. It turns out that or(in lowercase) doeswork, so there is clearly a bug in that code.

我也在使用 spring 2.5.6 并且在OR不工作时遇到了类似的问题,但AND正在工作。事实证明or(小写)确实有效,因此该代码中显然存在错误。

It is interesting that the proper aspectJ syntax is &&, ||, and !, but the and/or/not syntax was added by spring to make working in xml easier. From the manual:

有趣的是,正确的 aspectJ 语法是&&, || ,和,但 spring 添加了 and/or/not 语法以使在 xml 中工作更容易。从手册:

When combining pointcut sub-expressions, '&&' is awkward within an XML document, and so the keywords 'and', 'or' and 'not' can be used in place of '&&', '||' and '!' respectively.

当组合切入点子表达式时,'&&' 在 XML 文档中很笨拙,因此可以使用关键字 'and'、'or' 和 'not' 代替 '&&'、'||' 和 '!' 分别。

I would guess that since &&is the only one that is actuallyawkward in xml (there is nothing difficult about putting | or ! in xml) then ANDis the only one that was properly tested

我猜想,既然&&是唯一一个在 xml 中实际上很尴尬的(在 xml 中放置 | 或 ! 没有什么困难),那么AND是唯一一个经过正确测试的