Spring建立一個Bean 耗時
阿新 • • 發佈:2019-01-02
applicationContext中只有一個bean
<bean id="testUnit" class="test.TestUnit" lazy-init="true">
</bean>
通過以下程式碼,進行測試。
執行結果:
2010-8-4 14:09:13 org.springframework.context.support.AbstractApplicationContext prepareRefresh
資訊: Refreshing org[email protected]5ffb18: startup date [Wed Aug 04 14:09:13 CST 2010]; root of context hierarchy
2010-8-4 14:09:13 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [test/applicationContext.xml]
2010-8-4 14:09:13 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
資訊: Pre-instantiating singletons in org.s [email protected]1d15445: defining beans [testUnit]; root of factory hierarchy
[b]16
0
0[/b]
以上可見,spring的beanFactory創造bean的用時,也是不少的。
<bean id="testUnit" class="test.TestUnit" lazy-init="true">
</bean>
通過以下程式碼,進行測試。
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"test/applicationContext.xml"},true);
// bean layz-init=true
long startTime = System.currentTimeMillis();
TestUnit testUnit = (TestUnit) context.getBean("testUnit", context
.getType("testUnit"));
System.out.println(System.currentTimeMillis()-startTime);
// 使用new建立
startTime = System.currentTimeMillis();
testUnit = new TestUnit();
System.out.println(System.currentTimeMillis()-startTime);
// spring已經例項化bean,獲取。
startTime = System.currentTimeMillis();
testUnit = context.getBean("testUnit",TestUnit.class);
System.out.println(System.currentTimeMillis()-startTime);
執行結果:
2010-8-4 14:09:13 org.springframework.context.support.AbstractApplicationContext prepareRefresh
資訊: Refreshing org[email protected]5ffb18: startup date [Wed Aug 04 14:09:13 CST 2010]; root of context hierarchy
2010-8-4 14:09:13 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [test/applicationContext.xml]
2010-8-4 14:09:13 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
資訊: Pre-instantiating singletons in org.s
[b]16
0
0[/b]
以上可見,spring的beanFactory創造bean的用時,也是不少的。