依赖注入的方式测试ArrayList和LinkedList的效率

先贴结果



项目结构



使用配置文件的方式

package com.baobaotao1;
import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    //往这个类里面注入了2个对象
    private List<Integer> list01;
    private List<Integer> list02;

    public void setList01(List<Integer> list01) {
        this.list01 = list01;
    }
    public void setList02(List<Integer> list02) {
        this.list02 = list02;
    }
  public  void vs(List<Integer> list){
        long t1 = new Date().getTime();
        list.add(1); list.add(2);list.add(3);
        for(int i=1;i<99999;i++){
            list.add(1,i);
        }
        long t2 = new Date().getTime();
        System.out.println("时间是"+(t2-t1));
    } 
  public void test(){
    this.vs(list01);
    this.vs(list02);
  }
  public static void main(String[] args) {
    //new CarTest().test();
     //上面的这种测试的话,加载不了配置文件,空指针异常,在tomcat服务器中运行的web项目中会自动加载
      // web项目中,要么自动去加载xml文件,要么根据web.xml文件的配置去加载application.xml文件
       ApplicationContext ctx =
            new ClassPathXmlApplicationContext("applicationContext.xml");
       Test c = (Test) ctx.getBean("carTest");
       c.test();
    }

}
在WEB-INF 这个目录下,这个文件夹下的web.xml文件会被自动加载并读取,
回答:tomcat会自动加载web.xml文件,部署在tomcat的WEB-INF文件夹下的.xm文件,
只有web.xml文件会被自动加载,如在这个目录下还有其他的xml文件,需要在web.xml文件里配置

配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="list1" class="java.util.ArrayList" ></bean>
    <bean id="list2" class="java.util.LinkedList"></bean>

    <bean id="carTest" class="com.baobaotao1.CarTest">
        <property name="list01" ref="list1"/>
        <property name="list02" ref="list2"/>
    </bean>

</beans>




注解进行测试

package com.baobaotao1;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
/*下面的这2个注解用于    new ClassPathXmlApplicationContext*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @Resource //按照list01这个名称进行注入
    private List<Integer> list01;
    @Resource
    private List<Integer> list02;
    //面向接口编程List<Integer>完全降低了耦合,完全没有实现类的影子
    public  void vs(List<Integer> list){
        long t1 = new Date().getTime();
        list.add(1); list.add(2);list.add(3);
        for(int i=1;i<9999;i++){
            list.add(1,i);
        }
        long t2 = new Date().getTime();
        System.out.println("时间是"+(t2-t1));
    }

    public void test(){
        this.vs(list01);
        this.vs(list02);
    }

    public static void main(String[] args) {
        // new CarTest().test();  用这种方式去加载xml配置文件的话,也会空指针
         ApplicationContext ctx =
                 new ClassPathXmlApplicationContext("applicationContext.xml");
         Test c = (Test) ctx.getBean("carTest");
         c.test();
    }

}

配置文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!--这句话必须有,以及上面的那些必须有(这是进行注解开发所必须要的)-->
    <context:component-scan base-package="com.baobaotao1"></context:component-scan>

    <bean id="list01" class="java.util.ArrayList" ></bean>
    <bean id="list02" class="java.util.LinkedList"></bean>
    <bean id="carTest" class="com.baobaotao1.Test"></bean>
    //传入这个包名+类名 一般用到的都是反射机制
</beans>
时间: 2024-07-31 12:56:00

依赖注入的方式测试ArrayList和LinkedList的效率的相关文章

测试ArrayList和LinkedList的效率

测试代码如下 import java.util.ArrayList; import java.util.LinkedList; public class arraylistPKlinkedlist { public static void main(String[] args) { // TODO Auto-generated method stub arraylist(); linkedlist(); } static void arraylist(){ ArrayList al = new

MVVM模式用依赖注入的方式配置ViewModel并注册消息

最初的想法 这次主要讨论下给View指定ViewModel的事情.一般来说给View指定ViewModel常用的方式有两种,一种是在View的后台代码中写DataContext = new ViewModel(),还有一种是在XAML中指定View的DataContext.这两种方式都使得View对ViewModel产生了依赖,这种情况下可以考虑用依赖注入的方式使取消View对ViewModel的直接依赖.依赖注入一般来说可以通过构造函数注入.通过设置属性注入,这两种方法对于View来说都不合适

Spring 依赖注入的方式

Spring 支持3中依赖注入的方式 1.属性注入  通过setter 方法注入Bean的属性或依赖的对象. <bean id = " " class = " "> <property name = " " value = " "> </property>     <property name = " " value = " "> <

spring 配置bean的方法及依赖注入发方式

Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWord" class="com.spring.HelloWord"> <property name="userName" value="springsss"></property> </bean> 依

依赖注入及AOP简述(五)——依赖注入的方式 .

二.依赖注入的应用模式 前面我们了解了依赖注入的基本概念,也对一些依赖注入框架进行了简单的介绍,这一章我们主要来讨论作为开发者如何利用依赖注入框架来实现依赖注入的设计思想. 1.     依赖注入的方式 前面我们提到,所谓“依赖”,最简单地去解释就是一个Java类里的成员变量.我们都知道,给一个类中的私有成员变量赋值的方法通常有:通过Constructor构造方法.通过Setter方法.通过反射机制将私有变量的可见性设为true这三种方法.同样道理,依赖注入框架也是利用这三种方式来完成依赖对象的

EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下?

前言 这个问题从未遇见过,是一位前辈问我EF Core内存泄漏问题时我才去深入探讨这个问题,刚开始我比较惊讶,居然还有这种问题,然后就有了本文,直接拿前辈的示例代码并稍加修改成就了此文,希望对在自学EF Core过程中的童鞋能有些许帮助. EntityFramework Core内存泄漏回顾 接下来我将用简单示例代码来还原整个造成EntityFramework Core内存泄漏的过程,同时在这个过程中您也可思考一下其中的原因和最终的结果是否一致. public class TestA { pub

spring依赖注入的方式(一)

为了方便类的管理,spring提供了依赖注入的思想:类的实例化不由程序员控制,而是交给sprig容器进行管理. spring提供了多种类型的注入方式---注解.xml注入: 1  注解注入 有两种:@Autowired @Resource.两种都可以实现注入,但是二者的差别也很明显. @Resource 默认按照名称注入,在找不到名称的时候,会继续通过类型匹配: @Autowired 默认按照类型注入: @Resource 注解是由J2EE提供,而@Autowired是由spring提供,故为了

spring学习 五 依赖注入的方式

依赖注入有两种方式: 1 构造注入,如果<bean>标签下使用<contructor-arg>,则是构造注入 2 setter注入,就是调用setter方法注入,如果<bean>标签下使用<property>标签,就是setter注入 2.1: 如果属性是基本数据类型或 String 等简单类型 <bean id="peo" class="com.bjsxt.pojo.People"> <proper

依赖注入的方式(DI)

方式: 接口注入: setter方法注入: 构造方法注入: 接口注入: public class ClassA { private InterfaceB clzB; public void doSomething() { Ojbect obj = Class.forName(Config.BImplementation).newInstance(); clzB = (InterfaceB)obj; clzB.doIt(); } -- } 解释一下上述的代码部分,ClassA依赖于Interfac