Spring的@Qualifier注解

以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-qualifier-annotation.html

可能会出现这种情况,当你创建多个相同类型的bean并且希望仅使用属性中的一个连接时。在这种情况下,您可以使用@Qualifier注解和@Autowired注解来通过指定要连接哪个确切的bean来消除混淆。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jsoft.testspring</groupId>
  <artifactId>testannotationqualifier</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testannotationqualifier</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Spring Core -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
     
    <!-- Spring Context -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    
  </dependencies>
</project>

Student.java:

package com.jsoft.testspring.testannotationqualifier;

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

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

Profile.java:

package com.jsoft.testspring.testannotationqualifier;

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

public class Profile {
    @Autowired
    @Qualifier("student2")
    private Student student;
    
    public void getAge(){
        System.out.println(this.student.getAge());
    }
    
    public void getName() {
        System.out.println(this.student.getName());
    }
}

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/>

   <bean id="student1" class="com.jsoft.testspring.testannotationqualifier.Student">
        <property name="name" value="Jim"/>
        <property name="age" value="27"/>
   </bean>
   
   <bean id="student2" class="com.jsoft.testspring.testannotationqualifier.Student">
        <property name="name" value="Jim2"/>
        <property name="age" value="18"/>
   </bean>
   
   <bean id="profile" class="com.jsoft.testspring.testannotationqualifier.Profile"></bean>

</beans>

App.java:

package com.jsoft.testspring.testannotationqualifier;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
           ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
           Profile profile = (Profile)applicationContext.getBean("profile");
           profile.getAge();
           profile.getName();
    }
}

测试结果:

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test12/testannotationqualifier

原文地址:https://www.cnblogs.com/EasonJim/p/6899370.html