Hi Folks,
I'm new to spring, and learning it from Apress Prospring3 book.
I'm trying an example from that book on "Lookup Method Injection" which demonstrates that new instance of bean is created during each call of lookup method.
But When I tried the example, I don't see new instance is created for each call? Could someone point me what is wrong?
Here is the code:
Here is code output, which shows new instance is not created for each of getHelper() on abstractJobToDo instance.Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="helper" class="com.adi.prospring.ch4.methodInjection.MyHelper" /> <bean id="abstractJobToDo" class="com.adi.prospring.ch4.methodInjection.AbstractJobToDo"> <lookup-method name="getHelper" bean="helper" /> </bean> <bean id="standardJobToDo" class="com.adi.prospring.ch4.methodInjection.StandardJobToDo"> <property name="helper"> <ref local="helper" /> </property> </bean> </beans> ================================= package com.adi.prospring.ch4.methodInjection; public class MyHelper { public void doSomeThing() { } } ================================== package com.adi.prospring.ch4.methodInjection; public interface JobToDo { public MyHelper getHelper(); public void doIt(); } =================================== package com.adi.prospring.ch4.methodInjection; public abstract class AbstractJobToDo implements JobToDo { @Override public abstract MyHelper getHelper(); @Override public void doIt() { getHelper().doSomeThing(); } } ===================================== package com.adi.prospring.ch4.methodInjection; public class StandardJobToDo implements JobToDo { private MyHelper helper; @Override public MyHelper getHelper() { return this.helper; } @Override public void doIt() { getHelper().doSomeThing(); } public void setHelper(MyHelper helper) { this.helper = helper; } } ========================= Main class: package com.adi.prospring.ch4.methodInjection; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StopWatch; public class ExecuteJobs { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/adi/prospring/ch4/methodInjection/xmlBeanFactory.xml"); JobToDo standardJobToDo = ctx.getBean("standardJobToDo", JobToDo.class); JobToDo abstractJobToDo = ctx.getBean("abstractJobToDo", JobToDo.class); displayInfo(standardJobToDo); displayInfo(abstractJobToDo); } private static void displayInfo(JobToDo jobToDo) { StopWatch stopwatch = new StopWatch(); stopwatch.start("methodInjectionDemo"); MyHelper helper1 = jobToDo.getHelper(); MyHelper helper2 = jobToDo.getHelper(); System.out.println(jobToDo.getClass().getName() + " : is helper1==helper2? :" + (helper1 == helper2)); System.out.println("helper1=" + helper1); System.out.println("helper2=" + helper2); for (int x = 0; x < 100000; x++) { MyHelper helper = jobToDo.getHelper(); helper.doSomeThing(); } jobToDo.doIt(); stopwatch.stop(); System.out.println("Time elapsed=" + stopwatch.getTotalTimeMillis() + "ms"); } } ==============================
Code:com.adi.prospring.ch4.methodInjection.StandardJobToDo : is helper1==helper2? :true helper1=com.adi.prospring.ch4.methodInjection.MyHelper@558352d8 helper2=com.adi.prospring.ch4.methodInjection.MyHelper@558352d8 Time elapsed=5ms com.adi.prospring.ch4.methodInjection.AbstractJobToDo$$EnhancerByCGLIB$$7f0cfb5f : is helper1==helper2? :true helper1=com.adi.prospring.ch4.methodInjection.MyHelper@558352d8 helper2=com.adi.prospring.ch4.methodInjection.MyHelper@558352d8 Time elapsed=149ms


Reply With Quote