Spring 注入內(nèi)部 Beans

2022-05-21 15:23 更新

正如你所知道的 Java 內(nèi)部類(lèi)是在其他類(lèi)的范圍內(nèi)被定義的,同理,inner beans 是在其他 bean 的范圍內(nèi)定義的 bean。因此<property />或<constructor-arg />元素中的<bean />元素稱(chēng)為內(nèi)部bean,如下所示。

<?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="outerBean" class="...">
      <property name="target">
         <bean id="innerBean" class="..."/>
      </property>
   </bean>

</beans>

例子

我們?cè)谶m當(dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來(lái)創(chuàng)建一個(gè) Spring 應(yīng)用程序:

步驟 描述
1 創(chuàng)建一個(gè)名稱(chēng)為 SpringExample 的項(xiàng)目,并且在創(chuàng)建項(xiàng)目的 src 文件夾中創(chuàng)建一個(gè)包 com.tutorialspoint 。
2 使用 Add External JARs 選項(xiàng),添加所需的 Spring 庫(kù),解釋見(jiàn) Spring Hello World Example 章節(jié)。 option as explained in the chapter.
3 com.tutorialspoint 包中創(chuàng)建Java類(lèi)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();
   }
}

下面是另一個(gè)依賴(lài)的類(lèi)文件 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();
   }
}

下面是使用內(nèi)部 bean 為基于 setter 注入進(jìn)行配置的配置文件 Beans.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">

   <!-- Definition for textEditor bean using inner bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <property name="spellChecker">
         <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"/>
       </property>
   </bean>

</beans>

一旦你創(chuàng)建源代碼和 bean 配置文件完成后,我們就可以運(yùn)行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,將輸出以下信息:

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)