Spring @Autowired 註解



@Autowired 註解提供了更細粒度的控制,用於指定自動裝配應該在哪裡以及如何完成。@Autowired 註解可以用於自動裝配setter方法上的Bean,就像@Required註解一樣,也可以用於建構函式、屬性或具有任意名稱和/或多個引數的方法。

@Autowired 在Setter方法上

您可以使用@Autowired註解在setter方法上,以擺脫XML配置檔案中的<property>元素。當Spring找到在setter方法上使用的@Autowired註解時,它會嘗試對該方法執行按型別自動裝配。

示例

讓我們準備好一個Eclipse IDE,並按照以下步驟建立一個Spring應用程式:

步驟 描述
1 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個包com.tutorialspoint
2 Spring Hello World 示例章節中所述,使用新增外部JAR選項新增所需的Spring庫。
3 com.tutorialspoint包下建立Java類TextEditorSpellCheckerMainApp
4 src資料夾下建立Beans配置檔案Beans.xml
5 最後一步是建立所有Java檔案和Bean配置檔案的內容,並按如下所述執行應用程式。

以下是TextEditor.java檔案的內容:

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   private SpellChecker spellChecker;

   @Autowired
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker( ) {
      return spellChecker;
   }
   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"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>

   <!-- Definition for textEditor bean without constructor-arg  -->
   <bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
   </bean>

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

</beans>

建立原始檔和Bean配置檔案後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:

Inside SpellChecker constructor.
Inside checkSpelling.

@Autowired 在屬性上

您可以使用@Autowired註解在屬性上,以擺脫setter方法。當您使用<property>傳遞自動裝配屬性的值時,Spring將自動使用傳遞的值或引用分配這些屬性。因此,在屬性上使用@Autowired後,您的TextEditor.java檔案將如下所示:

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   @Autowired
   private SpellChecker spellChecker;

   public TextEditor() {
      System.out.println("Inside TextEditor constructor." );
   }
   public SpellChecker getSpellChecker( ){
      return spellChecker;
   }
   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

以下是配置檔案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"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>

   <!-- Definition for textEditor bean -->
   <bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
   </bean>

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

</beans>

完成以上對原始檔和Bean配置檔案的兩個更改後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:

Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.

@Autowired 在建構函式上

您也可以將@Autowired應用於建構函式。建構函式@Autowired註解表示在建立Bean時應該自動裝配建構函式,即使在XML檔案中配置Bean時未使用<constructor-arg>元素。讓我們檢查下面的示例。

以下是TextEditor.java檔案的內容:

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   private SpellChecker spellChecker;

   @Autowired
   public TextEditor(SpellChecker spellChecker){
      System.out.println("Inside TextEditor constructor." );
      this.spellChecker = spellChecker;
   }
   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

以下是配置檔案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"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>

   <!-- Definition for textEditor bean without constructor-arg  -->
   <bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
   </bean>

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

</beans>

完成以上對原始檔和Bean配置檔案的兩個更改後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

@Autowired 使用(required = false)選項

預設情況下,@Autowired註解意味著依賴項是必需的,類似於@Required註解,但是,您可以使用(required=false)選項關閉@Autowired的預設行為。

即使您沒有為age屬性傳遞任何值,以下示例仍然可以工作,但它仍然需要name屬性。您可以自己嘗試這個示例,因為它類似於@Required註解示例,只是Student.java檔案已更改。

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class Student {
   private Integer age;
   private String name;

   @Autowired(required=false)
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   
   @Autowired
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
}
spring_annotation_based_configuration.htm
廣告