鍍金池/ 教程/ Java/ Spring @Qualifier 注釋
Spring MVC Hello World 例子
事務管理
JDBC 框架概述
MVC 框架教程
Spring 自動裝配 ‘byName’
Spring 中的自定義事件
Spring 編程式事務管理
環(huán)境設(shè)置
概述
Spring 中 SQL 的存儲過程
體系結(jié)構(gòu)
Spring 中的事件處理
Spring 靜態(tài)頁面例子
基于注解的配置
依賴注入
Spring 自動裝配 ‘byType’
Spring 由構(gòu)造函數(shù)自動裝配
Spring @Qualifier 注釋
Spring ApplicationContext 容器
Spring 中基于 AOP 的 XML架構(gòu)
Bean 的生命周期
IoC 容器
注入內(nèi)部 Beans
Spring JDBC 示例
Spring 基于構(gòu)造函數(shù)的依賴注入
Spring @Required 注釋
Hello World 實例
Bean 定義
Sping 的 BeanFactory 容器
Spring 頁面重定向例子
Bean 定義繼承
Spring 中基于 AOP 的 @AspectJ
注入集合
Beans 自動裝配
Spring 異常處理例子
Spring 聲明式事務管理
Spring @Autowired 注釋
基于 Java 的配置
Spring MVC 表單處理例子
Spring——Bean 后置處理器
使用 Log4J 記錄日志
Spring 基于設(shè)值函數(shù)的依賴注入
Spring JSR-250 注釋
Bean 的作用域
Spring 框架的 AOP

Spring @Qualifier 注釋

可能會有這樣一種情況,當你創(chuàng)建多個具有相同類型的 bean 時,并且想要用一個屬性只為它們其中的一個進行裝配,在這種情況下,你可以使用 @Qualifier 注釋和 @Autowired 注釋通過指定哪一個真正的 bean 將會被裝配來消除混亂。下面顯示的是使用 @Qualifier 注釋的一個示例。

示例

讓我們使 Eclipse IDE 處于工作狀態(tài),請按照下列步驟創(chuàng)建一個 Spring 應用程序:

步驟 描述
1 創(chuàng)建一個名為 SpringExample 的項目,并且在所創(chuàng)建項目的 src 文件夾下創(chuàng)建一個名為 com.tutorialspoint 的包。
2 使用 Add External JARs 選項添加所需的 Spring 庫文件,就如在 Spring Hello World Example 章節(jié)中解釋的那樣。
3 com.tutorialspoint 包下創(chuàng)建 Java 類 Student,ProfileMainApp。
4 src 文件夾下創(chuàng)建 Beans 配置文件 Beans.xml
5 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并且按如下解釋的那樣運行應用程序。

這里是 Student.java 文件的內(nèi)容:

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 文件的內(nèi)容:

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 文件的內(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");
      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