Spring创建一个Bean 耗时

applicationContext中只有一个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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@1d15445: defining beans [testUnit]; root of factory hierarchy
[b]16
0
0[/b]

以上可见,spring的beanFactory创造bean的用时,也是不少的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值