Spring @Qualifier 註解



可能存在這樣一種情況:您建立了多個相同型別的 Bean,並且只想將其中一個與某個屬性關聯。在這種情況下,您可以將@Qualifier註解與@Autowired一起使用,透過指定要關聯的確切 Bean 來消除混淆。以下是一個展示@Qualifier註解用法的示例。

示例

讓我們準備好一個正在執行的 Eclipse IDE,並採取以下步驟建立一個 Spring 應用程式:

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

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

package com.tutorialspoint;

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

   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
}

以下是Profile.java檔案的內容

package com.tutorialspoint;

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

public class Profile {
   @Autowired
   @Qualifier("student1")
   private Student student;

   public Profile(){
      System.out.println("Inside Profile constructor." );
   }
   public void printAge() {
      System.out.println("Age : " + student.getAge() );
   }
   public void printName() {
      System.out.println("Name : " + student.getName() );
   }
}

以下是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");

      Profile profile = (Profile) context.getBean("profile");
      profile.printAge();
      profile.printName();
   }
}

考慮以下配置檔案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-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <!-- Definition for profile bean -->
   <bean id = "profile" class = "com.tutorialspoint.Profile"></bean>

   <!-- Definition for student1 bean -->
   <bean id = "student1" class = "com.tutorialspoint.Student">
      <property name = "name" value = "Zara" />
      <property name = "age" value = "11"/>
   </bean>

   <!-- Definition for student2 bean -->
   <bean id = "student2" class = "com.tutorialspoint.Student">
      <property name = "name" value = "Nuha" />
      <property name = "age" value = "2"/>
   </bean>

</beans>

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

Inside Profile constructor.
Age : 11
Name : Zara
spring_annotation_based_configuration.htm
廣告