Spring 注解注入—@Qualifier 注释

栏目: IT技术 · 发布时间: 3年前

内容简介:当创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用--------构造方法------------age:张三

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

1.这里是Student.java文件的内容:

 1 package com.spring.chapter7;
 2 
 3 public class Student {
 4     public String getName() {
 5         return name;
 6     }
 7     public void setName(String name) {
 8         this.name = name;
 9     }
10     public int getAge() {
11         return age;
12     }
13     public void setAge(int age) {
14         this.age = age;
15     }
16     private String name;
17     private int age;
18     
19 
20 
21 }

2.这里是Profile.java文件的内容:

 1 package com.spring.chapter7;
 2 
 3 import java.util.List;
 4 import java.util.Set;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.beans.factory.annotation.Qualifier;
 8 import org.springframework.beans.factory.annotation.Required;
 9 
10 
11 
12 public class Profile {
13     
14     @Autowired
15     @Qualifier("Student")    //这里的值就是bean的id,当我们改变此值跟随我们指定的bean进行注入
16     private Student student;
17     
18     public void printName() {
19         System.out.println("age:"+student.getName());
20     }    
21     
22     public void printAge() {
23         System.out.println("name:"+student.getAge());
24     }
25 
26     public Profile() {
27         System.out.println("--------构造方法------------");
28     }
29 }

3.下面是MainApp.java文件的内容:

 1 package com.spring.chapter7;
 2 
 3 
 4 import java.util.List;
 5 
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.AbstractApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 public class Main {
11     /**
12      * Spring @Qualifier  注解注入
13      * author:
14      * mail:2536201485@qq.com
15      * 时间:
16      */
17 
18     public static void main(String[] args) {
19         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring_xml/spring.xml");
20         Profile profile=(Profile)applicationContext.getBean("Profile");
21         profile.printName();
22         profile.printAge();
23         
24         
25     }
26 
27 }

4.下面配置文件 spring.xml的示例:

 1     <!-- @Qualifier 注解 -->
 2     <bean id="Profile">
 3     </bean>
 4     <bean id="Student">
 5         <property name="name" value="张三"></property>
 6         <property name="age" value="23"></property>
 7     </bean>
 8     
 9     <bean id="Student2">
10         <property name="name" value="李四"></property>
11         <property name="age" value="22"></property>
12     </bean>

运行结果:

--------构造方法------------

age:张三

name:23


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法设计、分析与实现

算法设计、分析与实现

徐子珊 / 2012-10 / 65.00元

《算法设计、分析与实现:c、c++和java》由徐子珊编著,第1章~第6章按算法设计技巧分成渐增型算法、分治算法、动态规划算法、贪婪算法、回溯算法和图的搜索算法。每章针对一些经典问题给出解决问题的算法,并分析算法的时间复杂度。这样对于初学者来说,按照算法的设计方法划分,算法思想的阐述比较集中,有利于快速入门理解算法的精髓所在。一旦具备了算法设计的基本方法,按应用领域划分专题深入学习,读者可以结合已......一起来看看 《算法设计、分析与实现》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码