日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Spring教程:Spring基于構(gòu)造函數(shù)的依賴注入

當(dāng)容器調(diào)用帶有一組參數(shù)的類構(gòu)造函數(shù)時(shí),基于構(gòu)造函數(shù)的 DI 就完成了,其中每個(gè)參數(shù)代表一個(gè)對(duì)其他類的依賴。

接下來,我們將通過示例來理解 Spring 基于構(gòu)造函數(shù)的依賴注入。

示例:

下面的例子顯示了一個(gè)類 TextEditor,只能用構(gòu)造函數(shù)注入來實(shí)現(xiàn)依賴注入。

讓我們用 Eclipse IDE 適當(dāng)?shù)毓ぷ?,并按照以下步驟創(chuàng)建一個(gè) Spring 應(yīng)用程序。

步驟 描述
1創(chuàng)建一個(gè)名為 SpringExample 的項(xiàng)目,并在創(chuàng)建的項(xiàng)目中的 src 文件夾下創(chuàng)建包 com.tutorialspoint 。
2使用 Add External JARs 選項(xiàng)添加必需的 Spring 庫,解釋見 Spring Hello World Example chapter.
3com.tutorialspoint 包下創(chuàng)建 Java類 TextEditor,SpellCheckerMainApp。
4src 文件夾下創(chuàng)建 Beans 的配置文件 Beans.xml
5最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容并按照如下所示的方法運(yùn)行應(yīng)用程序。

這是 TextEditor.java 文件的內(nèi)容:

package com.tutorialspoint;
public class TextEditor {
   private SpellChecker spellChecker;
   public TextEditor(SpellChecker spellChecker) {
      System.out.println("Inside TextEditor constructor." );
      this.spellChecker = spellChecker;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

下面是另一個(gè)依賴類文件 SpellChecker.java 的內(nèi)容:

package com.tutorialspoint;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   } 
}

以下是 MainApp.java 文件的內(nèi)容:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   }
}

下面是配置文件 Beans.xml 的內(nèi)容,它有基于構(gòu)造函數(shù)注入的配置:





   
   
      
   

   
   
   


當(dāng)你完成了創(chuàng)建源和 bean 配置文件后,讓我們開始運(yùn)行應(yīng)用程序。如果你的應(yīng)用程序運(yùn)行順利的話,那么將會(huì)輸出下述所示消息:

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

構(gòu)造函數(shù)參數(shù)解析:

注釋:上面這個(gè)例子里,將依賴類 SpellChecker.java注入到TextEditor.java 文件。

如此,便稱為依賴注入。

如果存在不止一個(gè)參數(shù)時(shí),當(dāng)把參數(shù)傳遞給構(gòu)造函數(shù)時(shí),可能會(huì)存在歧義。要解決這個(gè)問題,那么構(gòu)造函數(shù)的參數(shù)在 bean 定義中的順序就是把這些參數(shù)提供給適當(dāng)?shù)臉?gòu)造函數(shù)的順序就可以了。

考慮下面的類:

package x.y;
public class Foo {
   public Foo(Bar bar, Baz baz) {
      // ...
   }
}

下述配置文件工作順序:


   
      
      
   

   
   

讓我們再檢查一下我們傳遞給構(gòu)造函數(shù)不同類型的位置。考慮下面的類:

package x.y;
public class Foo {
   public Foo(int year, String name) {
      // ...
   }
}

如果你使用 type 屬性顯式的指定了構(gòu)造函數(shù)參數(shù)的類型,容器也可以使用與簡單類型匹配的類型。例如:



   
      
      
   


最后并且也是最好的傳遞構(gòu)造函數(shù)參數(shù)的方式,使用 index 屬性來顯式的指定構(gòu)造函數(shù)參數(shù)的索引。下面是基于索引為 0 的例子,如下所示:



   
      
      
   


最后,如果你想要向一個(gè)對(duì)象傳遞一個(gè)引用,你需要使用 標(biāo)簽的 ref 屬性,如果你想要直接傳遞值,那么你應(yīng)該使用如上所示的 value 屬性。


網(wǎng)站欄目:創(chuàng)新互聯(lián)Spring教程:Spring基于構(gòu)造函數(shù)的依賴注入
分享路徑:http://www.5511xx.com/article/djedpoh.html