Lookup Performance : IoC Injection « Spring « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Spring » IoC InjectionScreenshots 
Lookup Performance

/*
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>
    <bean id="factoryLookupBean" class="BeanFactoryAwareLookupDemoBean"/>
</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.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

public class BeanFactoryAwareLookupDemoBean implements BeanFactoryAware,
        DemoBean {

    private BeanFactory factory = null;

    public void setBeanFactory(BeanFactory factorythrows BeansException {
        this.factory = factory;
    }

    public MyHelper getMyHelper() {
        return (MyHelperfactory.getBean("helper");
    }

    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 LookupPerformance {

    public static void main(String[] args) {

        BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
                "build/lookup.xml"));

        DemoBean abstractBean = (DemoBeanfactory.getBean("abstractLookupBean");
        DemoBean factoryBean = (DemoBeanfactory.getBean("factoryLookupBean");

        testPerf(abstractBean);
        testPerf(factoryBean);
    }

    public static void testPerf(DemoBean bean) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("perfTest");

        for (int x = 0; x < 1000000; x++) {
            MyHelper helper = bean.getMyHelper();
            helper.doSomethingHelpful();
        }

        stopWatch.stop();

        System.out.println("1000000 gets took "
                + stopWatch.getTotalTimeSeconds() " seconds");
    }
}
           
       
LookupPerformance.zip( 1,479 k)
Related examples in the same category
1.Simple Inject By ID
2.Simple Inject By Name
3.Auto Wiring
4.Dependant Check
5.Hello World Xml With DI
6.Model View Injection in Spring
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.