Spring 注解注入—@Qualifier 注释
当创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱。下面显示的是使用 @Qualifier 注释的一个示例。
1.这里是 Student.java 文件的内容:
package com.spring.chapter7;
public class Student {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
}
2.这里是 Profile.java 文件的内容:
package com.spring.chapter7; import java.util.List;
import java.util.Set; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required; public class Profile { @Autowired
@Qualifier("Student") //这里的值就是bean的id,当我们改变此值跟随我们指定的bean进行注入
private Student student; public void printName() {
System.out.println("age:"+student.getName());
} public void printAge() {
System.out.println("name:"+student.getAge());
} public Profile() {
System.out.println("--------构造方法------------");
}
}
3.下面是 MainApp.java 文件的内容:
package com.spring.chapter7; import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
/**
* Spring @Qualifier 注解注入
* author:
* mail:2536201485@qq.com
* 时间:
*/ public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring_xml/spring.xml");
Profile profile=(Profile)applicationContext.getBean("Profile");
profile.printName();
profile.printAge(); } }
4.下面配置文件 spring.xml 的示例:
<!-- @Qualifier 注解 -->
<bean id="Profile" class="com.spring.chapter7.Profile">
</bean>
<bean id="Student" class="com.spring.chapter7.Student">
<property name="name" value="张三"></property>
<property name="age" value="23"></property>
</bean> <bean id="Student2" class="com.spring.chapter7.Student">
<property name="name" value="李四"></property>
<property name="age" value="22"></property>
</bean>
运行结果:
--------构造方法------------
age:张三
name:23
Spring 注解注入—@Qualifier 注释的更多相关文章
- 使用Spring注解注入属性
本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...
- spring注解注入:<context:component-scan>以及其中的context:include-filter>和 <context:exclude-filter>的是干什么的?
转自:https://www.cnblogs.com/vanl/p/5733655.html spring注解注入:<context:component-scan>使用说明 sprin ...
- spring注解注入properties配置文件
早期,如果需要通过spring读取properties文件中的配置信息,都需要在XML文件中配置文件读取方式. 基于XML的读取方式: <bean class="org.springf ...
- spring注解注入:<context:component-scan>使用说明
spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作.由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入. 在XML中配置了 ...
- spring注解注入:<context:component-scan>详解
spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作.由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入. 我们首先看一个注 ...
- spring注解注入的学习
spring在开发中起到管理bean的作用,不管是web应用还是应用软件开发.注入有多种方式,其中注解注入最为受欢迎.(java api 在1.6版本以上才引入关键的注解类如:@Resource) 配 ...
- JAVA框架 Spring 注解注入
一.首先需要引入jar包:spring-aop-4.2.4.RELEASE.jar.(在spring解压包libs内). 二.如果注解方式注入依赖的对象,需要引用新的约束. 内的:xsd-config ...
- spring 注解注入bean
通过注解方式注入bean,需要在配置类下注入bean 第一步,配置扫描文件夹 首先要在spring.xml中配置需要扫描的配置类 <context:componenet-scan base-pa ...
- spring注解注入失败一个原因
所有的注解看起来都没有任何问题,最后是由于web-xml配置问题. 由于缺少监听器org.springframework.web.context.ContextLoaderListener, 导致无法 ...
随机推荐
- javascript入门 之 用bootstrap-table写一个表格
方法1(对普通的 table 设置 data-toggle="table" 即可): <!DOCTYPE html> <html> <head> ...
- Linux中使用netstat命令的基本操作,排查端口号的占用情况
Linux中netstat命令详解 Netstat是控制台命令,是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实际的网络连接以及每一个网络接口设备的状态信息.Netstat用于显示与I ...
- tf.nn.softmax_cross_entropy_with_logits 分类
tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 参数: logits:就是神经网络最后一层的输出,如果有batch ...
- 动手学Transformer
动手实现Transformer,所有代码基于tensorflow2.0,配合illustrated-transformer更香. 模型架构 Encoder+Decoder Encoder Decode ...
- Problem L. World Cup
题目大意:有A,B,C,D四个队伍,两两之间有一个比赛,假如A和B比赛,如果平局,各加一分,如果说A胜,给A加3分,不给B加分,B胜同理 给出A,B,C,D,的得分,判断形成这种局面有多少种方式. 思 ...
- Servlet 和 Servlet容器
Servlet 很多同学可能跟我一样始终没有搞清楚到底什么是 Servlet,什么是 Servlet 容器.网上看了很多帖子,或许人家说的很清楚,但是自己的那个弯弯就是拐不过来. 想了很久说一下自己的 ...
- 排序1 - 选择排序 & 插入排序
请原谅我没有按照之前图片的分类来介绍排序算法,先说最简单的两种排序算法(冒泡略过),选择排序和插入排序,之前老是容易记混.默认输出升序的序列啊,哈哈. 选择排序 对于输入长度为n的数组,一共比较n-1 ...
- SpringCloud(一)学习笔记之项目搭建
[springcloud项目名称不支持下划线] 一.创建父项目 File---new---project: 填写项目信息: 默认即可,点击finish创建完成: 由于父项目只用到pom文件 所以把sr ...
- 最新VMware虚拟机安装Linux系统-CentOS(详细教程)
一.前言 最近有网友反应初学Linx不会安装,找了许多教程不是太全面,总会遇到一些要不是启动不了,要不是连不上网,各种问题,为了让大家能够顺利的安装,小乐写了一个非常详细的教程,让大家少入坑. 二.背 ...
- Des对称可逆加密
/// <summary> /// DES AES Blowfish /// 对称加密算法的优点是速度快, /// 缺点是密钥管理不方便,要求共享密钥. /// 可逆对称加密 密钥长 ...