PDA

View Full Version : AfterReturningAdvice


sheen022
02-24-2006, 06:48 PM
I'm using the AfterReturningAdvice for some my methods and have found it to be really useful.

There is, however, one issue I haven't been able to figure out and I was wondering if there is a workaround.

public String methodA() {
System.out.println("This is the methodA");
methodB();

}

public String methodB() {
System.out.println("This is the methodB");
}

Is it possible to have the advice invoke both methods? Since methodA is calling methodB (methodB should be invoked - after returning), and then after methodA returns - the advice to should be invoked for methodA.

I have been unable to get the advice invoke on methodB.
Please let me know if there is workaround.

Thanks - I appreciate it!

Aleks Seovic
02-25-2006, 05:37 AM
Hi,

It is posible, but at the moment it requires you to change how you invoke methodB from methodA.

Basically, the issue here is that you call methodA through proxy, but then, because both methodA and methodB are on the same target object, and call to methodB is basically equivalent to 'this.methodB', proxy is bypassed during that call and methodB is invoked directly on the target object.

The workaround you can use right now is to explicitly invoke methodB through proxy:


public String methodA() {
System.out.println("This is the methodA");
((MyProxiedInterface) AopContext.CurrentProxy).methodB();

}


Even though I agree while this is not the most elegant solution and will make your code dependent on Spring.Aop, that's the only thing you can do now to avoid raw reference problem.

As soon as I finish changes in the web framework I'm currently working on and we get 1.1 P3 release out, I will start working on a different AOP proxy implementation that will solve this problem and allow proxy to kick in even if you call methods directly.

However, the new solution will require you to make your methods virtual, so proxy can override them and add necessary advices, so you might want to plan ahead for that and start making methods that you know you want to advise virtual now.

Regards,

Aleks

sheen022
02-28-2006, 03:21 PM
Thanks Aleks. I'll def try that out.