Spring课程 Spring入门篇 4-4 Spring bean装配(下)之Autowired注解说明3 多选一 qualifier
本节主要讲述以下内容:
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的更多相关文章
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- Spring实践系列-入门篇(一)
本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...
- Spring学习十----------Bean的配置之Autowired注解实现
© 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ...
- Spring Cloud Alibaba入门篇
学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
- Spring Boot源码(四):Bean装配
为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...
- Spring课程 Spring入门篇 2-1 IOC和bean容器
课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...
- spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】
[ 前言] Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...
- Spring boot添加配置类@Configuration并初始化@Bean,@Resource和@Autowired都为null
大写加黑,找了好久@Resource和@Autowired都依赖不到创建的bean的原因:@Bean的方法名即是创建的Bean名称 import org.activiti.engine.Process ...
随机推荐
- uoj #111. 【APIO2015】Jakarta Skyscrapers
#111. [APIO2015]Jakarta Skyscrapers 印尼首都雅加达市有 NN 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 00 到 N−1N−1.除了这 NN 座摩 ...
- 15、OpenCV Python 轮廓发现
__author__ = "WSX" import cv2 as cv import numpy as np # 基于拓扑结构来发现和绘制(边缘提取) # cv.findConto ...
- ajax(Asynchronous JavaScript and XML) 异步js或者xml
1.XMLHttpRequest 对象:向服务器发送局部的请求,异步获取执行 a.浏览器支持 b.语法: xmlhttp==new XMLHttpRequest(); xmlhttp.open(&qu ...
- linux opencv版本查询
查看opencv安装路径: sudo find / -iname "*opencv*" 查看opencv版本: pkg-config opencv --modversion 查看o ...
- HTTP记录
-------------TCP握手协议------------- 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. [第一次握手]建立连接时,客户端发送syn包(syn ...
- window 系统 cygwin swool 问题
cygwin 终端乱码 端口占用 查看 tcp 端口: netstat -tno 或者 netstat -an | grep 端口 杀死进程号 : kill 进程号 ---> 143 ...
- abp 后台项目在IIS 中运行
安装 Current .NET Core Hosting Bundle installer (direct download)
- 洛谷 P3191 [HNOI2007]紧急疏散EVACUATE(网络最大流)
题解 二分答案+Dinic最大流 二分答案\(mid\) 把门拆成\(mid\)个时间点的门 相邻时间的门连一条\(inf\)的边 预处理出每个门到每个人的最短时间 为\(dis[k][i][j]\) ...
- 最近闲着利用QQ协议写了一个聊天器
最近闲着,把以前一个利用QQ协议写了的聊天器找出来玩,采用的是QQ比较稳定的协议,之前听说有人用WEB协议,或是安卓版QQ协议,都不太稳定.而我这个版的已经有好几年没动了.今天找出来依旧能登陆.获取好 ...
- [原创]c# 类中 Collection 字段初始化的特殊之处
1.今天看一下StackExchange.Redis的源代码,里面有这样一段代码 public sealed class ConfigurationOptions : ICloneable { ... ...