- Spring DI 教程
- Spring DI - 首頁
- Spring DI - 概述
- Spring DI - 環境搭建
- Spring DI - IOC 容器
- Spring 依賴注入
- Spring DI - 建立專案
- 基於構造器的注入示例
- Spring DI - 基於構造器的注入
- Spring DI - 內部 Bean 構造器注入
- Spring DI - 集合型別構造器注入
- Spring DI - 集合引用型別構造器注入
- Spring DI - Map 型別構造器注入
- Spring DI - Map 引用型別構造器注入
- 基於 Setter 的注入示例
- Spring DI - 基於 Setter 的注入
- Spring DI - 內部 Bean Setter 注入
- Spring DI - 集合型別 Setter 注入
- Spring DI - 集合引用型別 Setter 注入
- Spring DI - Map 型別 Setter 注入
- Spring DI - Map 引用型別 Setter 注入
- 自動裝配示例
- Spring DI - 自動裝配
- Spring DI - 按名稱自動裝配
- Spring DI - 按型別自動裝配
- Spring DI - 按構造器自動裝配
- 工廠方法
- Spring DI - 靜態工廠方法
- Spring DI - 非靜態工廠方法
- Spring DI 有用資源
- Spring DI - 快速指南
- Spring DI - 有用資源
- Spring DI - 討論
Spring DI - 按型別自動裝配
此模式指定按屬性型別自動裝配。Spring 容器查詢在 XML 配置檔案中將autowire屬性設定為byType的 Bean。然後,它嘗試匹配並連線屬性,如果其型別與配置檔案中 Bean 名稱中的一個完全匹配。如果找到匹配項,它將注入這些 Bean。否則,將不會連線 Bean。
例如,如果在配置檔案中將 Bean 定義設定為按型別自動裝配,並且它包含一個spellChecker屬性(型別為SpellChecker),Spring 將查詢名為SpellChecker的 Bean 定義,並使用它來設定該屬性。您仍然可以使用<property>標籤連線其餘屬性。以下示例將說明此概念。
示例
以下示例顯示了一個類TextEditor,它只能使用純基於 Setter 的注入進行依賴注入。
讓我們更新在Spring DI - 建立專案章節中建立的專案。我們新增以下檔案:
TextEditor.java - 包含 SpellChecker 作為依賴項的類。
SpellChecker.java - 依賴類。
MainApp.java - 執行和測試的主應用程式。
以下是TextEditor.java檔案的內容:
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
private String name;
public void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
以下是另一個依賴類檔案SpellChecker.java的內容:
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}
以下是MainApp.java檔案的內容:
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("applicationcontext.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
以下是具有按名稱自動裝配配置的配置檔案applicationcontext.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 -->
<bean id = "textEditor" class = "com.tutorialspoint.TextEditor" autowire = "byType">
<property name = "name" value = "Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>
輸出
建立原始檔和 Bean 配置檔案後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:
Inside SpellChecker constructor. Inside checkSpelling.
廣告