依賴註入的方式測試ArrayList和LinkedList的效率
阿新 • • 發佈:2017-08-22
xml文件 pri port 以及 實現 根據 也會 pat this
先貼結果
項目結構
使用配置文件的方式
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 = newClassPathXmlApplicationContext("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的效率