课程链接:

本节主要讲述以下内容:

1    简述

2    代码演练

2.1  注解qualifier运用

1    简述

1.1  何种情况使用qualifier注解?

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

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

1.2  xml方式如何运用qualifier

  1. <bean class="com.ddwei.bean">
  2.  
  3. <qualifier value="main"/>
  4.  
  5. </bean>

2    代码演练

2.1  注解qualifier运用

实体类:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.stereotype.Component;
  9.  
  10. @Component
  11. public class BeanInvoker {
  12. @Autowired
  13. private List<BeanInterface> list;
  14.  
  15. @Autowired
  16. private Map<String, BeanInterface> map;
  17.  
  18. @Autowired
  19. @Qualifier("beanImplOne")
  20. private BeanInterface bInterface;
  21. //
  22. public void say(){
  23. if(null!=list&&0!=list.size()){
  24. System.out.println("list...");
  25. for(BeanInterface bean :list){
  26. System.out.println(bean.getClass().getName());
  27. }
  28. }else{
  29. System.out.println("list is not null");
  30. }
  31.  
  32. if(null!=map&&0!=map.size()){
  33. System.out.println("map...");
  34. for(Map.Entry<String,BeanInterface> entry :map.entrySet()){
  35. System.out.println(entry.getKey()+" "+entry.getClass().getName());
  36. }
  37. }else{
  38. System.out.println("Map<String,BeanInterface> map is null");
  39. }
  40.  
  41. if(null!=bInterface){
  42. System.out.println(bInterface.getClass().getName());
  43. }else{
  44. System.out.println("bInterface is null");
  45. }
  46.  
  47. }
  48.  
  49. }

测试类:

  1. package com.imooc.beaninvoker;
  2.  
  3. import org.junit.Test;
  4.  
  5. import com.imooc.beanannotation.injection.service.InjectionService;
  6. import com.imooc.beanannotation.multibean.BeanInvoker;
  7. import com.imooc.test.base.UnitTestBase;
  8.  
  9. public class TestBeanInvoker extends UnitTestBase{
  10.  
  11. public TestBeanInvoker() {
  12. super("classpath*:spring-beaninvoker.xml");
  13. }
  14.  
  15. @Test
  16. public void testBeanInvoker(){
  17. try {
  18. BeanInvoker bInvoker = super.getbean("beanInvoker");
  19. bInvoker.say();
  20. } catch (Exception e) {
  21. // TODO: handle exception
  22. e.printStackTrace();
  23. }
  24. }
  25. }

xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <context:component-scan base-package="com.imooc.beanannotation"/>
  11.  
  12. </beans>

dao1:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import org.springframework.core.annotation.Order;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Order(2)
  7. @Component
  8. public class BeanImplOne implements BeanInterface{
  9.  
  10. }

dao2:

  1. package com.imooc.beanannotation.multibean;
  2.  
  3. import org.springframework.core.annotation.Order;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Order(1)
  7. @Component
  8. public class BeanImplTwo implements BeanInterface{
  9.  
  10. }

打印日志:

  1. 三月 20, 2019 6:39:04 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@789df61d: startup date [Wed Mar 20 06:39:04 CST 2019]; root of context hierarchy
  3. 三月 20, 2019 6:39:04 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beaninvoker.xml]
  5. list...
  6. 三月 20, 2019 6:39:06 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
  7. 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@789df61d: startup date [Wed Mar 20 06:39:04 CST 2019]; root of context hierarchy
  8. com.imooc.beanannotation.multibean.BeanImplTwo
  9. com.imooc.beanannotation.multibean.BeanImplOne
  10. map...
  11. beanImplOne java.util.LinkedHashMap$Entry
  12. beanImplTwo java.util.LinkedHashMap$Entry
  13. 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. uoj #111. 【APIO2015】Jakarta Skyscrapers

    #111. [APIO2015]Jakarta Skyscrapers 印尼首都雅加达市有 NN 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 00 到 N−1N−1.除了这 NN 座摩 ...

  2. 15、OpenCV Python 轮廓发现

    __author__ = "WSX" import cv2 as cv import numpy as np # 基于拓扑结构来发现和绘制(边缘提取) # cv.findConto ...

  3. ajax(Asynchronous JavaScript and XML) 异步js或者xml

    1.XMLHttpRequest 对象:向服务器发送局部的请求,异步获取执行 a.浏览器支持 b.语法: xmlhttp==new XMLHttpRequest(); xmlhttp.open(&qu ...

  4. linux opencv版本查询

    查看opencv安装路径: sudo find / -iname "*opencv*" 查看opencv版本: pkg-config opencv --modversion 查看o ...

  5. HTTP记录

    -------------TCP握手协议------------- 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. [第一次握手]建立连接时,客户端发送syn包(syn ...

  6. window 系统 cygwin swool 问题

    cygwin 终端乱码  端口占用 查看 tcp  端口: netstat -tno  或者 netstat -an | grep 端口 杀死进程号 : kill   进程号 --->  143 ...

  7. abp 后台项目在IIS 中运行

    安装 Current .NET Core Hosting Bundle installer (direct download)

  8. 洛谷 P3191 [HNOI2007]紧急疏散EVACUATE(网络最大流)

    题解 二分答案+Dinic最大流 二分答案\(mid\) 把门拆成\(mid\)个时间点的门 相邻时间的门连一条\(inf\)的边 预处理出每个门到每个人的最短时间 为\(dis[k][i][j]\) ...

  9. 最近闲着利用QQ协议写了一个聊天器

    最近闲着,把以前一个利用QQ协议写了的聊天器找出来玩,采用的是QQ比较稳定的协议,之前听说有人用WEB协议,或是安卓版QQ协议,都不太稳定.而我这个版的已经有好几年没动了.今天找出来依旧能登陆.获取好 ...

  10. [原创]c# 类中 Collection 字段初始化的特殊之处

    1.今天看一下StackExchange.Redis的源代码,里面有这样一段代码 public sealed class ConfigurationOptions : ICloneable { ... ...