Spring 基于設(shè)值函數(shù)的依賴注入

2022-05-21 15:23 更新

當(dāng)容器調(diào)用一個(gè)無參的構(gòu)造函數(shù)或一個(gè)無參的靜態(tài) factory 方法來初始化你的 bean 后,通過容器在你的 bean 上調(diào)用設(shè)值函數(shù),基于設(shè)值函數(shù)的 DI 就完成了。

示例:

下述例子顯示了一個(gè)類 TextEditor,它只能使用純粹的基于設(shè)值函數(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.
3 com.tutorialspoint 包下創(chuàng)建 Java類 TextEditor,SpellCheckerMainApp
4 src 文件夾下創(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;
   // a setter method to inject the dependency.
   public void setSpellChecker(SpellChecker spellChecker) {
      System.out.println("Inside setSpellChecker." );
      this.spellChecker = spellChecker;
   }
   // a getter method to return spellChecker
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

在這里,你需要檢查設(shè)值函數(shù)方法的名稱轉(zhuǎn)換。要設(shè)置一個(gè)變量 spellChecker,我們使用 setSpellChecker() 方法,該方法與 Java POJO 類非常相似。讓我們創(chuàng)建另一個(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)容,該文件有基于設(shè)值函數(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">
      <property name="spellChecker" ref="spellChecker"/>
   </bean>

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

</beans>

你應(yīng)該注意定義在基于構(gòu)造函數(shù)注入和基于設(shè)值函數(shù)注入中的 Beans.xml 文件的區(qū)別。唯一的區(qū)別就是在基于構(gòu)造函數(shù)注入中,我們使用的是〈bean〉標(biāo)簽中的〈constructor-arg〉元素,而在基于設(shè)值函數(shù)的注入中,我們使用的是〈bean〉標(biāo)簽中的〈property〉元素。

第二個(gè)你需要注意的點(diǎn)是,如果你要把一個(gè)引用傳遞給一個(gè)對象,那么你需要使用 標(biāo)簽的 ref 屬性,而如果你要直接傳遞一個(gè)值,那么你應(yīng)該使用 value 屬性。

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

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

使用 p-namespace 實(shí)現(xiàn) XML 配置:

如果你有許多的設(shè)值函數(shù)方法,那么在 XML 配置文件中使用 p-namespace 是非常方便的。讓我們查看一下區(qū)別:

以帶有 標(biāo)簽的標(biāo)準(zhǔn) 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="john-classic" class="com.example.Person">
      <property name="name" value="John Doe"/>
      <property name="spouse" ref="jane"/>
   </bean>

   <bean name="jane" class="com.example.Person">
      <property name="name" value="John Doe"/>
   </bean>

</beans>

上述 XML 配置文件可以使用 p-namespace 以一種更簡潔的方式重寫,如下所示:

<?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: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">

   <bean id="john-classic" class="com.example.Person"
      p:name="John Doe"
      p:spouse-ref="jane"/>
   </bean>

   <bean name="jane" class="com.example.Person"
      p:name="John Doe"/>
   </bean>

</beans>

在這里,你不應(yīng)該區(qū)別指定原始值和帶有 p-namespace 的對象引用。-ref 部分表明這不是一個(gè)直接的值,而是對另一個(gè) bean 的引用。


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號