Spring AOP Pointcut syntax for AND, OR and NOT

Odinodin picture Odinodin · Feb 15, 2010 · Viewed 41.3k times · Source

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).

<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.

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).

Can anybody give any tips?

Answer

Declan picture Declan · May 7, 2010

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

<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