Tuesday 3 November 2015

Method Lookup injection in Spring 3.x

When we need to inject a prototype-scoped bean in a singleton-scoped bean. Since singletons are created (and then injected) during context creation: it's the only time the Spring context is accessed and thus prototype-scoped beans are injected only once, thus defeating their purpose. In order to inejct prototypes into singletons, and side-by-syde with setter and constructor injection, Spring proposes another way for injection, called method injection.


public abstract class Master implements BeanNameAware{ private String beanId; private String name; // The bean which you want to make prototype // inside singleton bean. return it from // abstract method // The abstract method getNewDogInstance in type Master can only set a // visibility modifier, one of public or // protected abstract public Dog getNewDogInstance(); public Master(){ System.out.println("____Master bean is created_____");     }}  public class Dog implements BeanNameAware{
private String beanId; private String name; private String color; private Date dob; public Dog(){ System.out.println("____Dog bean is created_____"); }


  
   }    <bean id="master" class="com.sp.core.model.Master" scope="singleton" init-method="callMeToConvert">     <property name="name" value="Dave"/>      <!-- abstract public Dog getNewDogInstance(); -->      <lookup-method name="getNewDogInstance" bean="tdog"/> </bean>   <bean id="tdog" class="com.sp.core.model.Dog" scope="prototype">     <property name="name" value="Samsung"/>     <property name="color" value="white"/>     <property name="dob" ref="cdate"/>   </bean>    <bean id="cdate" class="java.util.Date" scope="prototype"/>
    Another Implementation using  </aop:scoped-proxy>     @Scope(value = "prototype",proxyMode=ScopedProxyMode.TARGET_CLASS)   
@Scope(value = "prototype",proxyMode=ScopedProxyMode.TARGET_CLASS)@Service("Sim")public class Sim { private String sid = "S22100@!223"; private java.util.Date date = new java.util.Date(); @Override public String toString() { return "Sim [sid=" + sid + ", date=" + date + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; }}@Scope(value = "singleton", proxyMode = ScopedProxyMode.DEFAULT)@Service("HCT")public class HCT { private String name = "Magic"; @Autowired @Qualifier("Sim") private Sim sim; //Here we are autowiring proxy object of bean Sim
@Override public String toString() { return "HCT [name=" + name + ", sim=" + sim + "]";
}
}

Main codepublic static void main(String[] args) throws InterruptedException {
//Instantiating spring container ApplicationContext applicationContext=new ClassPathXmlApplicationContext("anno/hct-sim.xml"); System.out.println("ohhhhh929*#*##########################################"); System.out.println("ohhhhh929*#*##########################################"); System.out.println("ohhhhh929*#*##########################################"); System.out.println("ohhhhh929*#*##########################################"); System.out.println("ohhhhh929*#*##########################################"); HCT hct=(HCT)applicationContext.getBean("HCT"); Thread.sleep(2000); System.out.println(hct); hct=(HCT)applicationContext.getBean("HCT"); Thread.sleep(2000); System.out.println(hct); hct=(HCT)applicationContext.getBean("HCT"); Thread.sleep(2000); System.out.println(hct); hct=(HCT)applicationContext.getBean("HCT"); Thread.sleep(2000); System.out.println(hct); hct=(HCT)applicationContext.getBean("HCT"); System.out.println(hct); Thread.sleep(2000); }  

No comments:

Post a Comment