9/20 号再进行学习

在C++中,main函数尽可能的简单,只要调用子函数的一句话就实现了功能。

java开发中,controller就相同于是main函数,其他类的方法不在本类中时候,

1.可以用new对象的方法,调用那个方法

2.用依赖注入的方式,将其他类的对象注入到main函数的类中,然后就可以调用这个方法了

先贴结果


项目结构


8/22添加
链表的优势是插入和删除,劣势是查找
数组的优势的查找,劣势的插入删除
在9999999*2个整数中进行查找的效率对比
ArrayListTest used a total time is 1 millisecondes !(有时测出来是0)
linkedList used a total time is 15 millisecondes !

从序列中删除一个数字的效率对比
ArrayListTest used a total time is 15 millisecondes !
linkedList used a total time is 0 millisecondes !

使用配置文件的方式

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>

依赖注入的方式测试ArrayList和LinkedList的效率(对依赖注入的再次理解)的更多相关文章

  1. 老徐和阿珍的故事:ArrayList和LinkedList的效率到底哪个高?

    人物背景: 老徐,男,本名徐福贵,从事Java相关研发工作多年,职场老油条,摸鱼小能手,虽然岁数不大但长的比较着急,人称老徐.据说之前炒某币败光了所有家产,甚至现在还有欠债. 阿珍,女,本名陈家珍,刚 ...

  2. Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor

    Webservice WCF WebApi   注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...

  3. Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件

    1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...

  4. MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息

    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...

  5. java的list几种实现方式的效率(ArrayList、LinkedList、Vector、Stack),以及 java时间戳的三种获取方式比较

    一.list简介 List列表类,顺序存储任何对象(顺序不变),可重复. List是继承于Collection的接口,不能实例化.实例化可以用: ArrayList(实现动态数组),查询快(随意访问或 ...

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

    最初的想法 这次主要讨论下给View指定ViewModel的事情.一般来说给View指定ViewModel常用的方式有两种,一种是在View的后台代码中写DataContext = new ViewM ...

  7. 极简SpringBoot指南-Chapter02-Spring依赖注入的方式

    仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...

  8. 基准测试了 ArrayList 和 LinkedList ,发现我们一直用 ArrayList 也是没什么问题的

    ArrayList 应该是 Java 中最常用的集合类型了,以至于我们说到集合就会自然而然的想到 ArrayList.很多同学都没有用过除了 ArrayList 之外的其他集合,甚至于都已经忘了除了 ...

  9. ArrayList、LinkedList、Vector、Array和HashMap、HashTable

    就 ArrayList 与 Vector 主要从二方面来说. 一.同步性:Vector 是线程安全的,也就是说是同步的,而ArrayList 是线程序不安全的,不是同步的 二.数据增长:当需要增长时, ...

随机推荐

  1. 【转载】ZooKeeper学习第二期--ZooKeeper安装配置

    原文地址(https://www.cnblogs.com/sunddenly/p/4018459.html) 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及 ...

  2. Invalid MEX-file: caffe.mexa64 的解决方案

    http://blog.csdn.net/iamzhangzhuping/article/details/53105708

  3. C++基础知识:泛型编程

    1.泛型编程的概念 ---不考虑具体数据类型的编程模式Swap 泛型写法中的 T 不是一个具体的数据类型,而是泛指任意的数据类型. 2.函数模板 - 函数模板其实是一个具有相同行为的函数家族,可用不同 ...

  4. matlab运行中出现“Caught "std::exception" Exception message is: Message Catalog MATLAB:builtins was not loaded from the file."

    在我运行过程中,经常爆出这一不确定是什么的问题,经排查后发现,原来是fopen 文件后,没有及时fclose导致的.

  5. kubernetes 环境搭建(ubuntu16.04)

    通过kubeadm安装kubernetes的教程:1: 首先在每台机器上安装: docker(1.12), kubeadm(1.6), kubectl, kubelet, kubernetes-cni ...

  6. Effective Java Chapter4 Classes and Interface

    MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...

  7. Wrapper

    开放封闭原则: 开放对扩展 封闭修改源代码 改变了人家调用方式 装饰器结构 """ 默认结构为三层!!!每层返回下一层内存地址就可以进行执行函数, 传参:语法糖中的传参可 ...

  8. [学习日志]2018-11-18 主要: idea更改java版本

    idea更改java版本 问题: 解决办法:

  9. Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置

    Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...

  10. 大数据-04-Hbase入门

    本文主要来自于 http://dblab.xmu.edu.cn/blog/install-hbase/ 谢谢原作者 本指南介绍了HBase,并详细指引读者安装HBase. 前面第二章学习指南已经指导大 ...