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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Spring教程:Spring注入內(nèi)部Beans

正如你所知道的 Java 內(nèi)部類是在其他類的范圍內(nèi)被定義的,同理,inner beans 是在其他 bean 的范圍內(nèi)定義的 bean。因此元素中的元素稱為內(nèi)部bean,如下所示。





   
      
         
      
   

例子

我們在適當?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來創(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 章節(jié)。 option as explained in the chapter.
3com.tutorialspoint 包中創(chuàng)建Java類TextEditorSpellCheckerMainApp。
4src 文件夾中創(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;
   // 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();
   }
}

下面是另一個依賴的類文件 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 注入進行配置的配置文件 Beans.xml 文件:





   
   
      
         
       
   

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

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

名稱欄目:創(chuàng)新互聯(lián)Spring教程:Spring注入內(nèi)部Beans
文章轉(zhuǎn)載:http://m.5511xx.com/article/dpojhes.html