• Spring深入浅出(十),注解,@Qualifier


    可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱。

    一、创建实体Bean

    package com.clzhang.spring.demo;
    
    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;
        }
    }

    二、创建业务逻辑层

    package com.clzhang.spring.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    public class Profile {
        @Autowired
        @Qualifier("student2")
        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());
        }
    }

    三、创建主程序

    package com.clzhang.spring.demo;
    
    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();
       }
    }

    四、配置文件如下

    <?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.clzhang.spring.demo.Profile">
       </bean>
    
       <!-- Definition for student1 bean -->
       <bean id="student1" class="com.clzhang.spring.demo.Student">
          <property name="name"  value="Zara" />
          <property name="age"  value="11"/>
       </bean>
    
       <!-- Definition for student2 bean -->
       <bean id="student2" class="com.clzhang.spring.demo.Student">
          <property name="name"  value="Nuha" />
          <property name="age"  value="2"/>
       </bean>
    
    </beans>

    五、运行

    Inside Profile constructor.
    Age : 2
    Name : Nuha

    本文参考:

    https://www.w3cschool.cn/wkspring/knqr1mm2.html

  • 相关阅读:
    (五)Redis在项目中应用
    股票收益最大问题
    (四)redigo
    (三)go-sql-driver
    为什么TCP要3次握手?4次挥手?
    分支预测
    事务隔离级别说明
    剑指offer解题思路锦集11-20题
    C++中的二义性问题
    memcached Vs redis
  • 原文地址:https://www.cnblogs.com/nayitian/p/15005926.html
Copyright © 2020-2023  润新知