package com.liuxt.bean;
import junit.framework.TestCase;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;
public class ComputerTest extends TestCase {
public static void main(String[] args) {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { "bean/ComputerBean.xml" });
ComputerBean computer1=(ComputerBean)ctx.getBean("computer");
ComputerBean computer2=(ComputerBean)ctx.getBean("computer");
System.out.println("computer1===computer2:"+(computer1==computer2));
testGetBeanFromSpring(ctx);
testNewInstaniate();
ctx.destroy();
}
private static StopWatch testGetBeanFromSpring(AbstractApplicationContext ctx) {
StopWatch stopWatch=new StopWatch();
stopWatch.start();
ComputerBean computer=(ComputerBean)ctx.getBean("computer");
for(int i=0;i<100000;i++){
computer=(ComputerBean)ctx.getBean("computer");
computer.sum(i,i);
}
stopWatch.stop();
System.out.println("100000 gets took "+stopWatch.getTotalTimeMillis()+" ms");
return stopWatch;
}
private static void testNewInstaniate() {
ComputerBean computer3;
StopWatch stopWatch=new StopWatch();
stopWatch.start();
for(int i=0;i<100000;i++){
computer3=new ComputerBean();
computer3.sum(i,i);
}
stopWatch.stop();
System.out.println("100000 gets took "+stopWatch.getTotalTimeMillis()+" ms");
}
}
测试的前提条件:
JDK:java version "1.6.0_13"
winxp cpu:2.6Ghz
测试结果:
computer1===computer2:false
100000 gets took 907 ms
100000 gets took 0 ms
不知道为什么差距这么大。
请大家帮忙解释一下啊。。。期待中。
分享到:
相关推荐
概念:面向切面编程,在不改变源码的情况下对方法进行增强,抽取横切关注点(日志处理,事务管理,安全检查,性能测试等等),使用AOP进行增强,使程序员只需要关注与业务逻辑编写. 专业术语 目标Target:需要增强的类 ...
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> ...
性能也比hibernate好。而且也比较轻量级,因为当时在项目中,没来的及做很很多笔记。后来项目结束了,我也没写总结文档。已经过去好久了。但最近突然又对这个ORM 工具感兴趣。因为接下来自己的项目中很有可能采用这...
bean 工厂的概念是 Spring 作为 IOC 容器的基础。IOC 将处理事情的责任从应用程序代码转移到框架。正如我将在下一个示例中演示的那样,Spring 框架使用 JavaBean 属性和配置数据来指出必须设置的依赖关系。 ...
增加事务测试样例,详见AdminController的save方法,此坑很深,爬了一天,由于没有对spring事务的深入了解,导致事务一直不成功,原因在于spring事务只能处理没有被捕获的异常信息,如果对方法增加了事务,请尽量...
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" autowire="autodetect"> <property name="hibernateProperties"> <props> ...
12.3.2 CacheController的放入、获取以及清除操作 223 12.3.3 注册CacheController以供使用 224 12.4 配置iBATIS不支持的DataSource 224 12.5 定制事务管理 225 12.5.1 理解TransactionConfig接口 226 12.5.2 理解...
两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;在有状态SessionBean中,用累加器,以对话状态存储起来,创建EJB对象,并将...
两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;在有状态SessionBean中,用累加器,以对话状态存储起来,创建EJB对象,并将...
而越来越多专业知识的 Blog 的出现,让我们看到了 Blog 更多所蕴涵的巨大的信息价值:不同的 Blog 选择不同的内容,收集和整理成为很多人关注的专业 Blog ——目前越来越多的人获取信息的来源是一些固定的 Blog 。...