Spring AOP Pointcut syntax for AND, OR and NOT

房东的猫 提交于 2019-12-03 10:57:40

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 && ! instead of 'AND NOT' or '&& !'. I'm doing this in the xml file

<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

This should work (spring AOP reference):

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

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

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:

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!