View Full Version : Constructor called twice when using CGLIB proxy beans.
ram_2000
Aug 21st, 2004, 09:00 PM
Why is the constructor called twice when using CGLIB proxy beans.
I have a System.out.println in the constructor:
System.out.println("*** inside constructor : " + this);
I can see the following output in the console.
*** inside constructor : aop.classproxy.BusinessLogic@1be0f0a
*** inside constructor : aop.classproxy.BusinessLogic@1be0f0a
Thanks,
Ram.
ram_2000
Aug 21st, 2004, 09:27 PM
Update:
-----------
This doesn't happen when using "autoproxy"....but only when using the "ProxyFactoryBean"
gpoirier
Aug 21st, 2004, 09:45 PM
That's because CGLIB proxies are used like interface based proxies. There's two instances created, one is the real thing, and the other is just a delegate that contains the AOP hook and call the real instance for the real work. The delegate is a CGLIB class that extends the class that you want to proxy. However, the toString() method of the proxy actually delegates to the real object, that is why both times you see the real class, not the proxy class. But if you implements toString, make it final and return super.toString(), then you'll see that one of those call was in the cglib enhanced class, while the other was in you original class. Or just print getClass() in the constructor.
Guillaume
irbouho
Aug 21st, 2004, 10:10 PM
With this in mind, one should pay a lot of attention to the processing implemented in the constructor.
gpoirier
Aug 21st, 2004, 10:25 PM
Well, that's part of the reason why interface based proxy is generally preferered. Same goes for final methods. And it also apply to the super class.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.