课程链接:

本节主要讲述以下内容:

1    简述

2    代码演练

2.1  注解qualifier运用

1    简述

1.1  何种情况使用qualifier注解?

a  按类型自动装配多个bean实例,可以用@qualifier指定唯一

b  目标是构造器或一个多参方法时,最好使用qualifiers,否则用resource(只有一个参数的setter方法)

1.2  xml方式如何运用qualifier

<bean class="com.ddwei.bean">

<qualifier value="main"/>

</bean>

2    代码演练

2.1  注解qualifier运用

实体类:

package com.imooc.beanannotation.multibean;

import java.util.List;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; @Component
public class BeanInvoker {
@Autowired
private List<BeanInterface> list; @Autowired
private Map<String, BeanInterface> map; @Autowired
@Qualifier(
"beanImplOne")
private BeanInterface bInterface;
//
public void say(){
if(null!=list&&0!=list.size()){
System.out.println("list...");
for(BeanInterface bean :list){
System.out.println(bean.getClass().getName());
}
}else{
System.out.println("list is not null");
} if(null!=map&&0!=map.size()){
System.out.println("map...");
for(Map.Entry<String,BeanInterface> entry :map.entrySet()){
System.out.println(entry.getKey()+" "+entry.getClass().getName());
}
}else{
System.out.println("Map<String,BeanInterface> map is null");
} if(null!=bInterface){
System.out.println(bInterface.getClass().getName());
}else{
System.out.println("bInterface is null"
);
}
} }

测试类:

package com.imooc.beaninvoker;

import org.junit.Test;

import com.imooc.beanannotation.injection.service.InjectionService;
import com.imooc.beanannotation.multibean.BeanInvoker;
import com.imooc.test.base.UnitTestBase; public class TestBeanInvoker extends UnitTestBase{ public TestBeanInvoker() {
super("classpath*:spring-beaninvoker.xml");
} @Test
public void testBeanInvoker(){
try {
BeanInvoker bInvoker = super.getbean("beanInvoker");
bInvoker.say();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

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:component-scan base-package="com.imooc.beanannotation"/> </beans>

dao1:

package com.imooc.beanannotation.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Order(2)
@Component
public class BeanImplOne implements BeanInterface{ }

dao2:

package com.imooc.beanannotation.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Order(1)
@Component
public class BeanImplTwo implements BeanInterface{ }

打印日志:

三月 20, 2019 6:39:04 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@789df61d: startup date [Wed Mar 20 06:39:04 CST 2019]; root of context hierarchy
三月 20, 2019 6:39:04 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beaninvoker.xml]
list...
三月 20, 2019 6:39:06 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@789df61d: startup date [Wed Mar 20 06:39:04 CST 2019]; root of context hierarchy
com.imooc.beanannotation.multibean.BeanImplTwo
com.imooc.beanannotation.multibean.BeanImplOne
map...
beanImplOne java.util.LinkedHashMap$Entry
beanImplTwo java.util.LinkedHashMap$Entry
com.imooc.beanannotation.multibean.BeanImplOne

Spring课程 Spring入门篇 4-4 Spring bean装配(下)之Autowired注解说明3 多选一 qualifier的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring学习十----------Bean的配置之Autowired注解实现

    © 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ...

  4. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  5. Spring Data JPA 入门篇

    Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...

  6. Spring Boot源码(四):Bean装配

    为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...

  7. Spring课程 Spring入门篇 2-1 IOC和bean容器

    课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...

  8. spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

    [ 前言]  Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...

  9. Spring boot添加配置类@Configuration并初始化@Bean,@Resource和@Autowired都为null

    大写加黑,找了好久@Resource和@Autowired都依赖不到创建的bean的原因:@Bean的方法名即是创建的Bean名称 import org.activiti.engine.Process ...

随机推荐

  1. requests模块处理cookie,代理ip,基于线程池数据爬取

    引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的. 一.基于requests模块 ...

  2. linux系统安全及应用——系统引导和登录控制

    一.开关机安全控制 1)调整BIOS将第一引导设备设为当前系统所在硬盘 2)调整BIOS禁止从其他设备(光盘.U盘.网络)引导系统 3)调整BIOS将安全级别设为setup,并设置管理员密码 4)禁用 ...

  3. Spark调优秘诀——超详细

    版权声明:本文为博主原创文章,转载请注明出处. Spark调优秘诀 1.诊断内存的消耗 在Spark应用程序中,内存都消耗在哪了? 1.每个Java对象都有一个包含该对象元数据的对象头,其大小是16个 ...

  4. sql 语句设置主键

    创建表时候 SQL code? 1 2 3 4 CREATE TABLE tb ( id INT IDENTITY(1,1) PRIMARY KEY, ) 添加时候 SQL code? 1 2 ALT ...

  5. 为Arch Linux添加鼠标支持(gpm)

    gpm的安装 在Arch Linux中安装gpm $ pacman -S gpm 如果你正在使用触控板,需要安装一下插件 $ pacman -S gpm xf86-input-synaptics 需要 ...

  6. Qt 学习之路 2(71):线程简介

    Qt 学习之路 2(71):线程简介 豆子 2013年11月18日 Qt 学习之路 2 30条评论 前面我们讨论了有关进程以及进程间通讯的相关问题,现在我们开始讨论线程.事实上,现代的程序中,使用线程 ...

  7. COCO2018 stuff分割

    stuff何许人也,相对于目标而言的环境信息,一般是图像中的草地,墙面或者天空,因为往往在一张图像中这些背景占据着大部分像素,对于场景理解必不可少,所以引入了这一任务. 不过目前这个任务还没有发布te ...

  8. [BZOJ 4488][Jsoi2015]最大公约数

    传送门 不知谁说过一句名句,我们要学会复杂度分析 #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for( ...

  9. ASP.NET 设计模式分为三种类型

    设计模式分为三种类型,共23类.  一.创建型模式:单例模式.抽象工厂模式.建造者模式.工厂模式.原型模式.      二.结构型模式:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代 ...

  10. sql语句中变量的写法

        $sql = "update cat set num=num+1 where cat_id=$art[cat_id]";    $sql = "update ca ...