Lookup Demo : IoC Config : Spring : Java examples (example source code) Organized by topic

Java
C++
Java Home »  Spring   » [  IoC Config  ]  Screenshots 
 



Lookup Demo

/*
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 = (DemoBeanfactory.getBean("abstractLookupBean");
        DemoBean standardBean = (DemoBeanfactory.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");

    }
}
           
       
Download: LookupDemo.zip   ( 1,478  K )  
Related examples in the same category
1.  Simplest Usage of property config file In SpringHas Download File
2.  IoC XML ConfigHas Download File
3.  Load Bean Definition From XML FileHas Download File
4.  IoC in properties fileHas Download File








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.