Results 1 to 5 of 5

Thread: Constructor called twice when using CGLIB proxy beans.

  1. #1
    Join Date
    Aug 2004
    Location
    Vancouver, Canada
    Posts
    25

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Vancouver, Canada
    Posts
    25

    Default

    Update:
    -----------

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

  3. #3

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    With this in mind, one should pay a lot of attention to the processing implemented in the constructor.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5

    Default

    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.

Similar Threads

  1. problem when proxy class using CGLIB
    By Jiebawu in forum AOP
    Replies: 13
    Last Post: Jun 24th, 2006, 12:09 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. Other Hibernate DAO LazyInitializationExceptions
    By bernardsirius in forum Data
    Replies: 5
    Last Post: Feb 18th, 2005, 04:09 PM
  5. Replies: 9
    Last Post: Feb 8th, 2005, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •