Announcement Announcement Module
Collapse
No announcement yet.
Constructor called twice when using CGLIB proxy beans. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Constructor called twice when using CGLIB proxy beans.

    Why is the constructor called twice when using CGLIB proxy beans.

    I have a System.out.println in the constructor:
    Code:
    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.

  • #2
    Update:
    -----------

    This doesn't happen when using "autoproxy"....but only when using the "ProxyFactoryBean"

    Comment


    • #3
      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

      Comment


      • #4
        With this in mind, one should pay a lot of attention to the processing implemented in the constructor.

        Comment


        • #5
          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.

          Comment

          Working...
          X