麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Spring依賴注入三種方式詳解

2019-11-14 15:29:02
字體:
供稿:網(wǎng)友

在講解SPRing依賴注入之前的準(zhǔn)備工作:

  • 下載包含Spring的工具jar包的壓縮包
  • 解壓縮下載下來的Spring壓縮包文件
  • 解壓縮之后我們會(huì)看到libs文件夾下有許多jar包,而我們只需要其中的commons-logging-1.0.4.jar,spring-beans-4.2.1.RELEASE.jar,spring-context-4.2.1.RELEASE.jar,spring-context-support-4.2.1.RELEASE.jar,spring-core-4.2.1.RELEASE.jar,spring-expression-4.2.1.RELEASE.jar
  • 在Eclipse建立一個(gè)Dynamic Web Project
  • 將上述必要的幾個(gè)jar包導(dǎo)入WEB-INF文件夾下lib文件夾中
  • 在src下建立下面提到的包和類

準(zhǔn)備工作做完之后我們就開始依賴注入之旅了:

  • 很多人都有迷惑,到底什么是依賴注入,什么是控制反轉(zhuǎn),其實(shí)所說的依賴注入(DI)和控制反轉(zhuǎn)(IOC)是同一概念,他們是不分家的。
  • 通俗來講就是,當(dāng)某個(gè)角色(調(diào)用者)需要另一個(gè)角色(被調(diào)用者)的協(xié)助時(shí),在java中,通常需要調(diào)用者去創(chuàng)建被調(diào)用者的實(shí)例,即new一個(gè)被調(diào)用者的對(duì)象,而在Spring中,創(chuàng)建被調(diào)用者的工作不由調(diào)用者完成,因此稱控制反轉(zhuǎn);而創(chuàng)建被調(diào)用者的實(shí)例由Spring容器來完成,然后注入到調(diào)用者,因此也稱依賴注入
  • Spring采用配置文件或Annotation(注解)來管理Bean的實(shí)現(xiàn)類、依賴關(guān)系,Spring容器則根據(jù)配置文件,利用反射來創(chuàng)建實(shí)例,并為之注入依賴關(guān)系。
  • 打個(gè)比方吧,在古代生產(chǎn)力及低的社會(huì),人們自給自足,去打獵要自己生產(chǎn)工具Arrow(箭)來捕獲自己的獵物,完全是自己動(dòng)手制造(new)一個(gè)箭(對(duì)象)。到后來隨著社會(huì)的發(fā)展,有了制造箭工具的工廠,人們無需自己制造箭,而是去工廠告訴他們你需要什么,然后工廠就會(huì)給你相應(yīng)的工具。再后來,你無須到工廠,坐在家里就可以要什么發(fā)出個(gè)指令,箭就會(huì)出現(xiàn),這里人和箭都是有Spring來管理的,二者依賴關(guān)系由Spring提供。

 好了,廢話不多說了,下面即將進(jìn)入我們的正題,依賴注入的三種方式(setter注入、構(gòu)造注入、接口注入):

  先建立三種方式都會(huì)用到的類和接口

    接口:Arrow(箭)、Person(人)

    實(shí)現(xiàn)類:ArrowImpl、PersonImpl

    測(cè)試類:MainTest

  • setter注入

    Arrow接口:   

package iocdi;public interface Arrow {	public String getArrow();}

    Person接口:

package iocdi;public interface Person {	public void hunt();}

    ArrowImpl類:

package iocdi;public class ArrowImpl implements Arrow {	@Override	public String getArrow() {		return "an arrow";	}}

    PersonImpl類:

package iocdi;public class PersonImpl implements Person {	private Arrow arrow;		@Override	public void hunt() {		System.out.println("I get " + arrow.getArrow() + " to hunt.");	}		//set注入一支箭	public void setArrow(Arrow arrow) {		this.arrow = arrow;	}}

    MainTest類:

package iocdi;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathxmlApplicationContext;/** * @author ForeverLover */public class MainTest {	public static void main(String[] args) {		ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");		System.out.println("-----------------setter注入-----------------");		Person p = ac.getBean("PersonImpl",PersonImpl.class);		p.hunt();	}}

    看到測(cè)試類會(huì)有疑問,ApplicationContext.xml從哪兒冒出來的,這里要說的就是Spring容器幫助我們?nèi)?chuàng)建實(shí)例對(duì)象bean,在進(jìn)程啟動(dòng)時(shí),Spring容器會(huì)自動(dòng)加載此配置文件,解析通過配置文件配置的bean并創(chuàng)建對(duì)應(yīng)類的實(shí)例,被調(diào)用者使用指定方式注入到調(diào)用者中,從而控制反轉(zhuǎn)和依賴注入。ApplicationContext.xml配置文件內(nèi)容如下:

<?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:tx="http://www.springframework.org/schema/tx"	xmlns:aop="http://www.springframework.org/schema/aop"	xsi:schemaLocation="	http://www.springframework.org/schema/beans 	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd	http://www.springframework.org/schema/tx	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd	http://www.springframework.org/schema/aop 	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">		<bean id="ArrowImpl" class="iocdi.ArrowImpl"/>		<bean id="PersonImpl" class="iocdi.PersonImpl">		<!-- setter注入 -->		<property name="arrow" ref="ArrowImpl"/>	</bean>	</beans>

    所有工作都完成了,然后從測(cè)試類MainTest的main方法執(zhí)行一下,從控制臺(tái)可以看到結(jié)果,是OK的:

    

  • 構(gòu)造注入

  setter注入講完了,現(xiàn)在我們來聊聊構(gòu)造注入了,所謂構(gòu)造注入就是在實(shí)例化對(duì)象的時(shí)候就把參數(shù)傳給這個(gè)對(duì)象,我們知道對(duì)于JavaBean都必須有構(gòu)造器,最少有一個(gè)無參構(gòu)造器,到這我們可以繼續(xù)下面的構(gòu)造注入,與setter注入不同之處在于,PersonImpl獲得ArrowImpl實(shí)例的方法,接口Arrow和Person不變,ArrowImpl類也不變,我們修改一下PersonImpl類和ApplicationContext.xml文件:

    修改后的PersonImpl類:

package iocdi;public class PersonImpl implements Person {	private Arrow arrow;		public PersonImpl() {}		public PersonImpl(Arrow arrow) {		this.arrow = arrow;	}		@Override	public void hunt() {		System.out.println("I get " + arrow.getArrow() + " to hunt.");	}	}

    修改后的ApplicationContext.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:tx="http://www.springframework.org/schema/tx"	xmlns:aop="http://www.springframework.org/schema/aop"	xsi:schemaLocation="	http://www.springframework.org/schema/beans 	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd	http://www.springframework.org/schema/tx	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd	http://www.springframework.org/schema/aop 	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">		<bean id="ArrowImpl" class="iocdi.ArrowImpl"/>		<bean id="PersonImpl" class="iocdi.PersonImpl">		<!-- 構(gòu)造注入 -->		<constructor-arg ref="ArrowImpl"/>	</bean>	</beans>

    然后再次運(yùn)行一下MainTest測(cè)試類,看結(jié)果,仍然可行:

    

  • 接口注入

  不同于setter注入和構(gòu)造注入,接口注入無需在xml文件里配置bean,而利用Java反射創(chuàng)建實(shí)現(xiàn)接口類的實(shí)例。

    讓我們來修改一下MainTest測(cè)試類和PersonImpl類:

    修改的PersonImpl類:

package iocdi;public class PersonImpl implements Person {	private Arrow arrow;		@Override	public void hunt() {		try {			Object obj = Class.forName("iocdi.ArrowImpl").newInstance();			arrow = (Arrow) obj;			System.out.println("I get " + arrow.getArrow() + " to hunt.");		} catch (InstantiationException e) {			e.printStackTrace();		} catch (IllegalaccessException e) {			e.printStackTrace();		} catch (ClassNotFoundException e) {			e.printStackTrace();		}	}	}

    修改的MainTest類:

package iocdi;/** * @author ForeverLover */public class MainTest {	public static void main(String[] args) {		try {			Object obj = Class.forName("iocdi.PersonImpl").newInstance();			Person p = (Person) obj;			System.out.println("-----------------接口注入-----------------");			p.hunt();		} catch (InstantiationException e) {			e.printStackTrace();		} catch (IllegalAccessException e) {			e.printStackTrace();		} catch (ClassNotFoundException e) {			e.printStackTrace();		}	}}

    運(yùn)行一下測(cè)試類,從控制臺(tái)得出結(jié)果:

    

  • Lookup注入

  說到這順便說一下Lookup方法注入吧,對(duì)于Lookup注入需要一下幾個(gè)類:

    Arrow類

    Person抽象類

    MainTest測(cè)試類

  創(chuàng)建Arrow類:

package iocdi;import java.util.Random;public class Arrow {		private String arrow;	private String[] arrows = {"aaaaArrow", "bbbbArrow", "ccccArrow","ddddArrow","eeeeArrow",			"ffffArrow","ggggArrow","hhhhArrow","iiiiArrow"};		public Arrow() {		this.arrow = arrows[new Random().nextInt(9)];	}		public void getArrow() {		System.out.println("I get a " + arrow);	}	}

  創(chuàng)建Person類:

package iocdi;public abstract class Person {		public abstract Arrow createArrow();		public Arrow getArrow() {		return new Arrow();	}	}

  創(chuàng)建MainTest類:

package iocdi;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author ForeverLover */public class MainTest {	public static void main(String[] args) {		ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");		Person p = ac.getBean("Person", Person.class);		Arrow arrow1 = p.getArrow();		Arrow arrow2 = p.getArrow();		System.out.println(arrow1.equals(arrow2));		System.out.println("------------I am a dividing line------------");		Arrow arrow3 = p.createArrow();		Arrow arrow4 = p.createArrow();		System.out.println(arrow3.equals(arrow4));	}}

  修改ApplicationContext.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:tx="http://www.springframework.org/schema/tx"	xmlns:aop="http://www.springframework.org/schema/aop"	xsi:schemaLocation="	http://www.springframework.org/schema/beans 	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd	http://www.springframework.org/schema/tx	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd	http://www.springframework.org/schema/aop 	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">		<bean id="Arrow" class="iocdi.Arrow"/>	    <bean id="Person" class="iocdi.Person">        <lookup-method name="createArrow" bean="Arrow" />    </bean>	</beans>

  最后測(cè)試一下,控制臺(tái)查看一下:

  

  先說一下,分隔線前面之所以會(huì)輸出false是因?yàn)閯?chuàng)建的兩個(gè)對(duì)象的引用不同,雖然這里打印出來是eeeeArrow和ddddArrow結(jié)果確實(shí)不同,因?yàn)槭请S機(jī)的,即便有可能結(jié)果相同,兩個(gè)對(duì)象的引用也不相同。但是分隔線下面無論如何創(chuàng)建的兩個(gè)對(duì)象的引用相同。大家也可能會(huì)疑惑,為什么在配置文件里配置的抽象類也可以實(shí)例化對(duì)象,并且抽象類中的抽象方法createArrow()并沒具體實(shí)現(xiàn)卻可以創(chuàng)建Arrow實(shí)例,這里就跟Spring容器有關(guān),其具體實(shí)現(xiàn)了abstarct類,如果createArrow()不是抽象方法,那abstract實(shí)現(xiàn)類也會(huì)覆蓋這個(gè)方法。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 91色一区二区三区 | 久久久成人精品视频 | 99最新网址 | 欧美黄色大片免费观看 | 国产亚洲精久久久久久蜜臀 | 日本中文字幕高清 | 精品国产一区二区三区四区在线 | 女教师~淫辱の动漫在线 | 91情侣在线偷精品国产 | 深夜网站在线观看 | 激情网站视频 | 在线观看国产免费视频 | 欧美成人一级 | 伦理三区 | 欧美激情性色生活片在线观看 | 精品一区二区三区免费 | 免费在线观看毛片视频 | 国产精品美女一区二区 | 欧洲精品久久 | 国产视频软件在线 | 欧美人成在线 | 99精品国产一区二区三区 | 国产一区二区三区欧美 | 护士hd欧美free性xxxx | 性看小视频 | 精品国产精品久久 | 一级做受大片免费视频 | 亚洲第一成人在线 | 久久久久一区二区三区四区五区 | 国产最新网站 | 黄色特级视频 | 欧美日韩综合视频 | 亚洲欧美日韩中文在线 | 免费在线观看成人网 | 欧美性猛交xxxxx按摩国内 | 国产成人强伦免费视频网站 | 国产精品久久久久久久久久免 | 欧美黑人一级 | 最新黄色电影网站 | 最新午夜综合福利视频 | 一区二区三高清 |