Spring 基于構(gòu)造函數(shù)的依賴注入

2022-09-28 13:55 更新

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

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

示例:

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

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

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

下面是另一個依賴類文件 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ù)注入的配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <constructor-arg ref="spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

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

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

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

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

如此,便稱為依賴注入。

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

考慮下面的類:

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

下述配置文件工作順序:

<beans>
   <bean id="foo" class="x.y.Foo">
      <constructor-arg ref="bar"/>
      <constructor-arg ref="baz"/>
   </bean>

   <bean id="bar" class="x.y.Bar"/>
   <bean id="baz" class="x.y.Baz"/>
</beans>

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

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

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

<beans>

   <bean id="exampleBean" class="examples.ExampleBean">
      <constructor-arg type="int" value="2001"/>
      <constructor-arg type="java.lang.String" value="Zara"/>
   </bean>

</beans>

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

<beans>

   <bean id="exampleBean" class="examples.ExampleBean">
      <constructor-arg index="0" value="2001"/>
      <constructor-arg index="1" value="Zara"/>
   </bean>

</beans>

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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號