/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
///////////////////////////////////////////////////////////////////////////////////////
//File: lookup.xml
///////////////////////////////////////////////////////////////////////////////////////
public interface DemoBean {
public MyHelper getMyHelper();
public void someOperation();
}
///////////////////////////////////////////////////////////////////////////////////////
public class MyHelper {
public void doSomethingHelpful() {
// do something!
}
}
///////////////////////////////////////////////////////////////////////////////////////
public class StandardLookupDemoBean implements DemoBean {
private MyHelper helper;
public void setMyHelper(MyHelper helper) {
this.helper = helper;
}
public MyHelper getMyHelper() {
return this.helper;
}
public void someOperation() {
helper.doSomethingHelpful();
}
}
///////////////////////////////////////////////////////////////////////////////////////
public abstract class AbstractLookupDemoBean implements DemoBean {
public abstract MyHelper getMyHelper();
public void someOperation() {
getMyHelper().doSomethingHelpful();
}
}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.StopWatch;
public class LookupDemo {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/lookup.xml"));
DemoBean abstractBean = (DemoBean) factory.getBean("abstractLookupBean");
DemoBean standardBean = (DemoBean) factory.getBean("standardLookupBean");
displayInfo(standardBean);
displayInfo(abstractBean);
}
public static void displayInfo(DemoBean bean) {
MyHelper helper1 = bean.getMyHelper();
MyHelper helper2 = bean.getMyHelper();
System.out.println("Helper Instances the Same?: "
+ (helper1 == helper2));
StopWatch stopWatch = new StopWatch();
stopWatch.start("lookupDemo");
for (int x = 0; x < 100000; x++) {
MyHelper helper = bean.getMyHelper();
helper.doSomethingHelpful();
}
stopWatch.stop();
System.out.println("100000 gets took " + stopWatch.getTotalTimeMillis()
+ " ms");
}
}
LookupDemo.zip( 1,478 k)