课程链接:

本节主要讲述以下内容:

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. 用shell脚本安装apache

    我们首先创建一个文件为test.sh,执行此文件的方法有以下四种方式: 1../test.sh(必须chmod赋予执行权限) 2.. test.sh 3.sourse test.sh 4.[shell ...

  2. [译文]Casperjs1.1.0参考文档-快速开始

    快速开始 只要casperjs被正确安装,你就可以开始写你的第一个脚本,你可以使用javascript或者coffiescript编译脚本. 提示: 如果你对javascript不是很熟悉,最好先看专 ...

  3. 【问题记录】Python运行报错:can only concatenate str (not "int") to str

    自己总是写程序时候用 + 拼接的时候忘记变量类型要一致,如下面 frame_num = "1" for i in range(1, frame_num + 1, 1): self. ...

  4. 导出table为Excel

    1.HTML <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=" ...

  5. Spring学习笔记(四)—— Spring中的AOP

    一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...

  6. LeetCode154.寻找旋转排序数组中的最小值 II

    154.寻找旋转排序数组中的最小值 II 描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). ...

  7. [BZOJ 5330][SDOI2018] 反回文串

    传送门 怎么说呢,一道不可多得的反演题吧,具体解释之后再补 #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) ...

  8. bzoj3261: 最大异或和 (可持久化trie树)

    题目链接 题解 看到异或和最大就应该想到01 trie树 我们记\(S_i\)为前i项的异或和 那么我们的目的是最大化\(S_n\)^\(x\)^\(S_{j-1}\) \((l <= j &l ...

  9. 【数学】【筛素数】Miller-Rabin素性测试 学习笔记

        Miller-Rabin是一种高效的随机算法,用来检测一个数$p$是否是素数,最坏时间复杂度为$\log^3 p$,正确率约为$1-4^{-k}$,$k$是检验次数. 一.来源     Mil ...

  10. Oracle DBMS_UTILITY.GET_HASH_VALUE

    DBMS_UTILITY.GET_HASH_VALUE(input, base, hash_size) 1.DBMS_UTILITY.GET_HASH_VALUE 对于确定的输入字符串,如果base和 ...