Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > AOP (Aspect Oriented Programming)

Reply
 
Thread Tools Display Modes
  #1  
Old Aug 26th, 2004, 02:21 PM
adepue adepue is offline
Senior Member
 
Join Date: Aug 2004
Posts: 229
Default AOP, Proxies, and method calling in same instance

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.
Reply With Quote
  #2  
Old Aug 26th, 2004, 03:54 PM
irbouho irbouho is offline
Senior Member
 
Join Date: Aug 2004
Location: Montréal, Canada
Posts: 848
Default

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();
I find this approch a little intrusive.
__________________
Omar Irbouh

Spring Modules Team
http://irbouh.blogspot.com/
Reply With Quote
  #3  
Old Jun 22nd, 2005, 07:37 AM
Laurent PETIT Laurent PETIT is offline
Junior Member
 
Join Date: Oct 2004
Posts: 11
Default

Quote:
Originally Posted by irbouho
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();
I find this approch a little intrusive.
Hello,

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 ?
Reply With Quote
  #4  
Old Jun 22nd, 2005, 08:38 AM
Andreas Senft Andreas Senft is offline
Senior Member
 
Join Date: Aug 2004
Posts: 2,715
Default

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
Reply With Quote
  #5  
Old Jun 22nd, 2005, 06:46 PM
oliverhutchison oliverhutchison is offline
Senior Member
 
Join Date: Aug 2004
Location: Melbourne, Australia
Posts: 335
Default

Quote:
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.
Unfortunately this is not possible. CGLIB proxies must delegate to the target object as it's not possible to add behaviour to an existing object. e.g.

Code:
class AdvisedFoo extends Foo {
    Foo target;

    void fooBar() {
        // do before advice
        // do around advice
        target.fooBar();
        // do after advice        
    }
}
As you can see CGLIB is really no help at all and Andreas solution is looking pretty nice.

Ollie
Reply With Quote
  #6  
Old Jun 22nd, 2005, 07:00 PM
wpoitras wpoitras is offline
Senior Member
 
Join Date: Feb 2005
Location: Boston, MA
Posts: 1,140
Default

Could you use a combination of BeanNameAware and BeanFactoryAware interfaces to retrieve a reference to itself?
Reply With Quote
  #7  
Old Jun 23rd, 2005, 03:42 AM
Andreas Senft Andreas Senft is offline
Senior Member
 
Join Date: Aug 2004
Posts: 2,715
Default

Quote:
Originally Posted by wpoitras
Could you use a combination of BeanNameAware and BeanFactoryAware interfaces to retrieve a reference to itself?
I think so. But then you are bound to Spring (and I think that's to be avoided).

Regards,
Andreas
Reply With Quote
  #8  
Old Jun 23rd, 2005, 05:04 PM
Laurent PETIT Laurent PETIT is offline
Junior Member
 
Join Date: Oct 2004
Posts: 11
Default

Quote:
Originally Posted by oliverhutchison
Quote:
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.
Unfortunately this is not possible. CGLIB proxies must delegate to the target object as it's not possible to add behaviour to an existing object. e.g.

...

As you can see CGLIB is really no help at all and Andreas solution is looking pretty nice.

Ollie
Since I still haven't done this myself, I now doubt that you're right.
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.
Reply With Quote
  #9  
Old Jun 23rd, 2005, 07:16 PM
oliverhutchison oliverhutchison is offline
Senior Member
 
Join Date: Aug 2004
Location: Melbourne, Australia
Posts: 335
Default

Quote:
So I think someone doesn't correctly interpret the way CGLib works
Laurent,

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
Reply With Quote
  #10  
Old Jul 16th, 2005, 07:54 AM
latham latham is offline
Junior Member
 
Join Date: Jul 2005
Posts: 1
Default Re: AOP, Proxies, and method calling in same instance

Quote:
Originally Posted by adepue
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.
So it is true, and should be documented. Today I've found a bug in my code that was caused by this behaviour.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT -5. The time now is 08:53 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.