Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP Pointcut syntax for AND, OR and NOT

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?

like image 440
Odinodin Avatar asked Feb 15 '10 15:02

Odinodin


People also ask

How do I declare a pointcut in Spring AOP?

In Spring AOP, a join point always represents a method execution. A pointcut is a predicate that matches the join points, and the pointcut expression language is a way of describing pointcuts programmatically.

What is pointcut expression in spring?

Pointcut is a set of one or more JoinPoint where an advice should be executed. You can specify Pointcuts using expressions or patterns as we will see in our AOP examples. In Spring, Pointcut helps to use specific JoinPoints to apply the advice.

What is the difference between a join point and a pointcut?

A Joinpoint is a point in the control flow of a program where the control flow can arrive via two different paths(IMO : that's why call joint). A Pointcut is a matching Pattern of Joinpoint i.e. set of join points.

What is pointcut expression?

Pointcut is an expression language of spring AOP which is basically used to match the target methods to apply the advice. It has two parts ,one is the method signature comprising of method name and parameters. Other one is the pointcut expression which determines exactly which method we are applying the advice to.


3 Answers

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

like image 85
Declan Avatar answered Sep 27 '22 23:09

Declan


This should work (spring AOP reference):

pointcut="execution(* x.y.z.ClassName.*(..))           && !execution(* x.y.x.ClassName.someMethod(..))" 
like image 39
crunchdog Avatar answered Sep 27 '22 23:09

crunchdog


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

like image 33
James Scriven Avatar answered Sep 27 '22 23:09

James Scriven