Announcement Announcement Module
Collapse
No announcement yet.
CGLIB proxy method calls constructor twice? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CGLIB proxy method calls constructor twice?

    Hi,

    Can someone please explain the statement on CGLIB proxy :-

    The constructor of your proxied object will be called twice. Why would the constructor be called twice?

    How is it different from jdk proxy creatiion method?

    -Hetal

  • #2
    Cglib proxy creates new class that extends from your target bean class and cglib proxy is an instance of that new class. Superclass constructor (target bean class constructor actually) is called on proxy object instantiation.

    Spring aop uses proxy-base approach, i.e. it creates target bean and proxy for it as two distinct objects, hence, target bean class constructor is called twice.

    Comment


    • #3
      Thanks for your reply.

      I have one doubt that as per what you say if the target bean constructor is being called twice, now if i have a singleton bean, which is not declared as final, then do we mean to say that AOP will not work with Singleton beans?

      May be this is a very silly question but just asked as it came to my mind.

      -Hetal

      Comment


      • #4
        Hetal,

        Singleton scope means that the single bean should be used for the bean definition by the container. That doesn't mean that another instance of that class can't be created. E.g. it's possible to declare two beans of the same class as singletons.

        Spring container uses AOP proxy instead of 'raw' beans for resolving dependencies, i.e. it injects the proxy instead of 'raw' beans to all other interested beans but the same proxy is reused for the same bean declaration.

        Comment


        • #5
          Thanks for your inputs.

          As per what i had understood was that the bean defined as Singleton are different from GOF Singleton pattern but atleast within the same application context you cannot have 2 Singleton beans refering to the same class as mentioned by you . and within the same context, it should share the instance available and not create a new one.

          Please correct me incase i am wrong.

          Comment


          • #6
            Hetal,

            The reference is quite clear about that:
            HTML Code:
            When a bean is a singleton, only one shared  instance of the bean will be managed, and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned by the Spring container.
            
            To put it another way, when you define a bean definition and it is scoped as a singleton, then the Spring IoC container will create exactly one instance of the object defined by that bean definition. This single instance will be stored in a cache of such singleton beans, and all subsequent requests and references for that named bean will result in the cached object being returned.
            ...
            Please be aware that Spring's concept of a singleton bean is quite different from the Singleton pattern as defined in the seminal Gang of Four (GoF) patterns book. The GoF Singleton hard codes the scope of an object such that one and only one instance of a particular class will ever be created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean. This means that if you define one bean for a particular class in a single Spring container, then the Spring container will create one and only one instance of the class defined by that bean definition.

            Comment


            • #7
              Denis,

              I am sorry but i think that i have not explained things to you clearly. My question was with rerence to your first response stating that "Spring aop uses proxy-base approach, i.e. it creates target bean and proxy for it as two distinct objects, hence, target bean class constructor is called twice. " With a reference to this statement i was wondering that when my target class is a singleton bean , how would it create another instance within the same container.

              I hope i am more clear now.

              Comment


              • #8
                'Singleton' scope feature means that single object is used within the single bean definition. AOP doesn't break that - it created 'raw' bean and proxy but uses proxy in all places target bean definition is necessary.

                Comment

                Working...
                X