| |
查询演示 |
|
/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
///////////////////////////////////////////////////////////////////////////////////////
//File: lookup.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helper" class="MyHelper" singleton="false"/>
<bean id="abstractLookupBean" class="AbstractLookupDemoBean">
<lookup-method name="getMyHelper" bean="helper"/>
</bean>
<bean id="standardLookupBean" class="StandardLookupDemoBean">
<property name="myHelper">
<ref local="helper"/>
</property>
</bean>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
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) |
Related examples in the same category |
|