zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    机器学习进阶-案例实战-答题卡识别判 1.cv2.getPerspectiveTransform(获得投射变化后的H矩阵) 2.cv2.warpPerspective(H获得变化后的图像) 3.cv2.approxPolyDP(近似轮廓) 4.cv2.threshold(二值变化) 7.cv2.countNonezeros(非零像素点个数)6.cv2.bitwise_and(与判断)
    机器学习进阶-案例实战-停车场车位识别-keras预测是否停车站有车
    机器学习进阶-案例实战-停车场车位识别
    Spark HA模式访问Hadoop HA下的数据
    Solr Cloud的搭建使用
    TensorFlow的安装
    spark-submit提交方式测试Demo
    时间迭代和BigDecimal操作
    TestCodis的工具代码
    Gson的应用测试
  • 原文地址:https://www.cnblogs.com/nayitian/p/15005926.html
Copyright © 2011-2022 走看看