依賴注入的兩種方式並附上例子
1.依賴注入:就是指程式在執行過程中,如果需要另外一個物件協助完成時,無需在程式碼中建立被呼叫者,而是依賴外部的注入獲取。
2.依賴注入的兩種方式:
設定注入:設定注入是通過setter方法注入被呼叫者的例項。
構造注入:利用構造方法來設定依賴注入的方式稱為構造注入。
3.接下來附上例子來說明設定注入
(1).首先,附上設定注入的例子,新建一個Java專案,配置好spring後,即可,專案結構圖如下圖所示:
(2).首先,新建2個介面,一個Animal介面,一個Person介面,其中Animal介面有一個抽象的用途方法,就是動物是用來幹嘛的,其中Person介面有一個飼養的方法。
Animal.java檔案程式碼如下:
- package com.inter;
- publicinterface Animal {
- void use();//定義抽象的用途方法
- }
Person.java檔案程式碼如下:
- package com.inter;
- publicinterface Person {
- void raise();//定義抽象的飼養方法
- }
(3).接著新建3個類,放在com.bean包下,分別為Dog、Cat、Businessman類,其中Dog、Cat類的use方法只輸出一句話,Businessman類中有個Animal物件屬性,並有其對應的setter方法,有一個raise方法,也是簡單的輸出和呼叫方法。
Dog.java檔案程式碼如下:
- package com.bean;
- import com.inter.Animal;
- publicclass Dog implements Animal{
- @Override
- publicvoid use() {
- // TODO Auto-generated method stub
- System.out.println("狗的用途是看家");
- }
- }
Cat.java檔案程式碼如下:
-
package com.bean;
- import com.inter.Animal;
- publicclass Cat implements Animal{
- @Override
- publicvoid use() {
- // TODO Auto-generated method stub
- System.out.println("貓的用途是捉老鼠");
- }
- }
Businessman.java檔案程式碼如下:
- package com.bean;
- import com.inter.Animal;
- import com.inter.Person;
- publicclass Businessman implements Person {
- private Animal animal;
- public Businessman() {
- }
- publicvoid setAnimal(Animal animal) {
- this.animal = animal;
- }
- @Override
- publicvoid raise() {
- // TODO Auto-generated method stub
- System.out.println("人飼養動物");
- animal.use();
- }
- }
(4).對上面的類在applicationContext.xml檔案中進行配置,程式碼如下:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <beanid="bus"class="com.bean.Businessman">
- <propertyname="animal">
- <refbean="dog"/>
- </property>
- </bean>
- <beanid="dog"class="com.bean.Dog"></bean>
- <beanid="cat"class="com.bean.Cat"></bean>
- </beans>
其中定義一個Businessman類的bean中,有個屬性property標籤,其中name對應Businessman類的Animal物件屬性,並且使用ref標籤引用了下面定義的Dog物件的id, 就這樣設定注入了dog物件。
(5).最後編寫測試類Test,程式碼如下:
- package com.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.inter.Person;
- publicclass Test {
- publicstaticvoid main(String[] args){
- ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
- Person p=(Person) ac.getBean("bus");
- p.raise();
- }
- }
通過查詢配置檔案來找到有id為bus的bean,然後把這個bean強制轉換為一個Person物件,再呼叫這個Person物件的raise方法,此時raise方法即為Businessman的raise方法,因為在Businessman類中使用了Animal物件屬性,並通過設定注入到了配置檔案中。
(6).執行後效果如下:
為什麼輸出的是狗的用途呢,因為我們把物件屬性通過設定注入的方式注入到配置檔案中,而配置檔案中的物件屬性引用的為Dog這個類。
4.接下來附上構造注入的例子,來說明構造注入
(1).首先,新建一個Java專案,配置好Spring環境,先附上這個專案的檔案結構圖:
(2).首先,定義兩個介面,分別為Axe和Person介面,放在com.inter包下,其中Axe(斧頭)介面定義了一個抽象的chop(砍)方法,其中Person(人)介面定義了一個使用斧頭的方法。
Axe.java檔案的程式碼如下:
- package com.inter;
- publicinterface Axe {
- String chop();
- }
Person.java檔案程式碼如下:
- package com.inter;
- publicinterface Person {
- void useAxe();
- }
(3).接著新建3個類,分別為Chinese、SteelAxe、StoneAxe類,放在com.bean包下,其中Chinese類實現Person介面,SteelAxe和StoneAxe類實現Axe介面,其中Chinese類有一個物件屬性,一個普通屬性,並通過構造方法傳入兩個屬性作為引數,其中SteelAxe、StoneAxe類裡有一個返回值為字串的方法。
Chinese.java檔案程式碼如下: