java - Autowiring by constructor in Spring creating more objects -


i creating demo understand how can inject prototype bean singleton bean using constructor autowiring. here code first bean is

public class independentbean {     private string independentname;     public independentbean()     {         system.out.println("independent called");     }      public string getindependentname() {         return independentname;     }      public void setindependentname(string independentname) {         this.independentname = independentname;     } } 

now creating independent bean

package com.sample.beans;

public abstract class dependentbean {     private independentbean d1;     private independentbean d2;     public dependentbean()     {         system.out.println("default constructor dependent");     }     public independentbean getd1() {         return d1;     }      public void setd1(independentbean d1) {         system.out.println("setting d1");         this.d1 = d1;     }      public independentbean getd2() {         return d2;     }      public void setd2(independentbean d2) {         system.out.println("setting d2");         this.d2 = d2;     }      private string name;      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public dependentbean(independentbean d1) {         system.out.println("with 1");         this.d1 = d1;     }      public dependentbean(independentbean d1, independentbean d2) {         this.d1 = d1;         this.d2 = d2;         system.out.println("with 2");     }  public abstract independentbean getindependent(); }  

here context.xml:

    <?xml version="1.0" encoding="utf-8"?>     <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context"         xmlns:aop="http://www.springframework.org/schema/aop"         xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">         <bean class="com.sample.beans.independentbean" id="firstin" scope="prototype">             <property name="independentname" value="firstindependent" />         </bean>          <bean class="com.sample.beans.dependentbean" id="autowirebyconstructor"             autowire="constructor">   <lookup-method name="getindependent" bean="firstin" />             </bean>     </beans> 

here main class

package com.sample.beans;  import org.springframework.context.support.classpathxmlapplicationcontext;  public class mainclass {     public static void main(string[] args) {         classpathxmlapplicationcontext ctx = new classpathxmlapplicationcontext(                 "classpath:context.xml");         ctx.getbean("autowirebyconstructor");         ctx.close();     }  } 

according tospring spec know when working autowiring constructor constructor satisfying dependencies called. in case constructor of independent bean should call 2 times.but in case constructor called 4 times. not getting clearly.please me understand ?

here output of code:

independent called
independent called
independent called
independent called
2

please me understand behaviour.

i found answer of question in spring docs.

while use look-up method injection, spring uses cglib proxies fulfill requirement.when spring uses cglib proxies constructor of target bean called 2 time. hence here in case since 2 proxies of independent bean created , constructor called 4 times.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -