天天看點

Spring(六)之自動裝配

一、自動裝配模型

下面是自動連接配接模式,可以用來訓示Spring容器使用自動連接配接進行依賴注入。您可以使用元素的autowire屬性為bean定義指定autowire模式。

Spring(六)之自動裝配
可以使用 byType 或者 constructor 自動裝配模式來連接配接數組和其他類型的集合。

自動裝配的局限性

當自動裝配始終在同一個項目中使用時,它的效果最好。如果通常不使用自動裝配,它可能會使開發人員混淆的使用它來連接配接隻有一個或兩個 bean 定義。不過,自動裝配可以顯著減少需要指定的屬性或構造器參數,但你應該在使用它們之前考慮到自動裝配的局限性和缺點。

Spring(六)之自動裝配

Spring 自動裝配 ‘byName’

這種模式由屬性名稱指定自動裝配。Spring 容器看作 beans,在 XML 配置檔案中 beans 的 auto-wire 屬性設定為 byName。然後,它嘗試将它的屬性與配置檔案中定義為相同名稱的 beans 進行比對和連接配接。如果找到比對項,它将注入這些 beans,否則,它将抛出異常。

例如,在配置檔案中,如果一個 bean 定義設定為自動裝配 byName,并且它包含 spellChecker 屬性(即,它有一個 setSpellChecker(...) 方法),那麼 Spring 就會查找定義名為 spellChecker 的 bean,并且用它來設定這個屬性。你仍然可以使用 <property> 标簽連接配接其餘的屬性。下面的例子将說明這個概念。

(1)編寫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();
       }
}      

(2)編寫SpellChecker.java

package com.tutorialspoint;
public class SpellChecker {
   public SpellChecker() {
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   }   
}      

(3)編寫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("Beans.xml");
          TextEditor te = (TextEditor) context.getBean("textEditor");
          te.spellCheck();
       }
}      

(4)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-4.0.xsd">

 <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="byName">
      <property name="name" value="Generic Text Editor" />
   </bean>

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

(5)運作MainApp.java的main方法

結果如圖:

Spring(六)之自動裝配

Spring 自動裝配 ‘byType’

這種模式由屬性類型指定自動裝配。Spring 容器看作 beans,在 XML 配置檔案中 beans 的 autowire 屬性設定為 byType。然後,如果它的 type 恰好與配置檔案中 beans 名稱中的一個相比對,它将嘗試比對和連接配接它的屬性。如果找到比對項,它将注入這些 beans,否則,它将抛出異常。

例如,在配置檔案中,如果一個 bean 定義設定為自動裝配 byType,并且它包含 SpellChecker 類型的 spellChecker 屬性,那麼 Spring 就會查找定義名為 SpellChecker 的 bean,并且用它來設定這個屬性。你仍然可以使用 <property> 标簽連接配接其餘屬性。下面的例子将說明這個概念,你會發現和上面的例子沒有什麼差別,除了 XML 配置檔案已經被改變。

byType示例與上述byName除了xml配置之外并無多大差別,隻需将xml配置改為這樣,運作對應的main方法即可:

<?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-4.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>      

Spring 由構造函數自動裝配

這種模式與 byType 非常相似,但它應用于構造器參數。Spring 容器看作 beans,在 XML 配置檔案中 beans 的 autowire 屬性設定為 constructor。然後,它嘗試把它的構造函數的參數與配置檔案中 beans 名稱中的一個進行比對和連線。如果找到比對項,它會注入這些 bean,否則,它會抛出異常。

例如,在配置檔案中,如果一個 bean 定義設定為通過構造函數自動裝配,而且它有一個帶有 SpellChecker 類型的參數之一的構造函數,那麼 Spring 就會查找定義名為 SpellChecker 的 bean,并用它來設定構造函數的參數。你仍然可以使用 <constructor-arg> 标簽連接配接其餘屬性。下面的例子将說明這個概念。

一、編寫TextEditor.java

package com.tutorialspoint;

public class TextEditor {
      private SpellChecker spellChecker;
       private String name;
       public TextEditor( SpellChecker spellChecker, String name ) {
          this.spellChecker = spellChecker;
          this.name = name;
       }
       public SpellChecker getSpellChecker() {
          return spellChecker;
       }
       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("Beans.xml");
          TextEditor te = (TextEditor) context.getBean("textEditor");
          te.spellCheck();
       }
}      

四、編寫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-4.0.xsd">

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

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

   
</beans>      

五、運作MainApp.java中的main方法

結果如下:

Spring(六)之自動裝配