Reputation: 8820
I am new to Spring AOP.Let me explain the question. I have class Circle
public class Circle {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Circle getName called");
return name;
}
}
Now i have set advice getterAdvice()
to call before
getName()
method from class
Circle
as show in following code.
@Before("allGetters()")
public void getterAdvice(JoinPoint joinPoint){
System.out.println("getter Advice is called");
((Circle)joinPoint.getTarget()).getName();
}
@Pointcut("execution(* com.example.Circle.get*(..))")
void allGetters(){}
Output when call getName:
getter Advice is called
Circle getName called
Circle getName called
Now i call getter method from Circle
ie getName()
.It will first execute advice method and then getName()
method.But i have called getName()
again from advice. Still it is not again executing advice and not goes in to infinite calls.How Spring AOP acts with such a call?.Does advice executes only once for one method?
The intent of this question is to only understand the execution flow and working of AOP in Spring 3.0.
Your response will be greatly appreciated.
Upvotes: 1
Views: 687
Reputation:
You are calling method on not a proxy method
((Circle)joinPoint.getTarget()).getName();
You have to see first proxy object concept in advice calling.You can refer Spring Docs.They have given clear information about proxy object
Upvotes: 1
Reputation: 124546
I suggest a read of the AOP chapter, especially the section explaining proxies.
In short spring uses proxies to apply AOP so methods going into the object will pass through the proxy and will be adviced. In your aspect you are doing a getTarget
which returns the actual target (the unproxied object', the plain instance of your Circle
object) and not the proxy.
Upvotes: 3