|
#1
|
|||
|
|||
|
Spring can use JDK's Proxy mechanism to apply AOP. However, is it not true that an object instance that has an aspect applied by JDK's Proxy mechanism bypasses the aspect when it invokes its own methods? In other words, when an outside class invokes my proxied class, it will have the benefit of the aspect. But, if the proxied instance invokes another method on itself, then the aspect will not be applied on the method call. I should probably test this before posting, but I strongly suspect it is true. If true, this should be well documented as it can lead to difficult bugs.
|
|
#2
|
|||
|
|||
|
Spring does not use bytecode instrumentation to implement AOP. So it is predictable that if a method of a proxied object calls an other method in the same object, the aspect will not be applied on the method call.
Spring allows however to apply the aspect on this method call by setting exposeProxy to True and using AopContext.currentProxy(). Code:
//inside method1 ((MyBean) AopContext.currentProxy()).method2(); |
|
#3
|
|||
|
|||
|
Quote:
Yeah, "a little" is quite an Euphemism, isn't it ? :-) I think this problem could be "partially" solved with the use of CLIB instead of Dynamic Proxies : since the target class will be subclassed, calls to this() will first cause the overriden method to execute, thus allowing the AOP mechanism to work. Of course there will still be some problems here with CGLib, since all the methods marked as "final" cannot be subclassed Do I have the point ? Or did I miss something ? |
|
#4
|
|||
|
|||
|
An alternative solution could be the following:
Introduce a "this" property (with getter and setter) and invoke methods on the same instance via the getter. So getThis().foo() instead of this.foo(). The backing property is by default "this" but might be overriden with the proxy (if the instance is being proxied). If the proxy instance is being injected, no call to AOP specific methods is required. This approach is still a bit intrusive but works with and without a proxy being present. Regards, Andreas |
|
#5
|
|||
|
|||
|
Quote:
Code:
class AdvisedFoo extends Foo {
Foo target;
void fooBar() {
// do before advice
// do around advice
target.fooBar();
// do after advice
}
}
Ollie |
|
#6
|
|||
|
|||
|
Could you use a combination of BeanNameAware and BeanFactoryAware interfaces to retrieve a reference to itself?
|
|
#7
|
|||
|
|||
|
Quote:
Regards, Andreas |
|
#8
|
|||
|
|||
|
Quote:
Please refer to this thread : http://forum.springframework.org/sho...vokes Question where it is said that, apparently, this is possible. So I think someone doesn't correctly interpret the way CGLib works. Mmmm ... think I will try it myself to be sure ;-p[/url] Last edited by robyn; May 14th, 2006 at 11:52 AM. |
|
#9
|
|||
|
|||
|
Quote:
it's quite simple. Using the standard Java APIs you can not dynamically add behaviour to an existing object instance or, for that matter, any loaded class. If you take the time to *think* this through and you will see that I must be correct. Ollie |
|
#10
|
|||
|
|||
|
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| RMI proxies | rjst | Remoting and JMS | 0 | Sep 13th, 2005 06:35 AM |
| Solution for Proxies and method calling in same instance | chhajed | AOP (Aspect Oriented Programming) | 7 | Aug 10th, 2005 03:25 AM |
| States of Hibernate objects passed to a business method | rhasselbaum | Data Access | 11 | Dec 8th, 2004 10:37 AM |
| Transaction proxies and method delegation | rhasselbaum | Core Container | 13 | Nov 30th, 2004 09:48 AM |