zoukankan      html  css  js  c++  java
  • 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 Data框架
    Flutter入门坑一Could not resolve com.android.tools.build:gradle:3.2.1.
    圆形图像
    MissingPluginException(No implementation found for method getDatabasesPath on channel com.tekartik.sqflite)
    flutter应用打包、修改图标、启动页和app名字
    sqflite常用操作
    Flutter解决神奇的ListView顶部多一段空白高度的问题
    Flutter-CircleAvatar圆形和圆角图片
    flutter 报错 DioError [DioErrorType.DEFAULT]: Bad state: Insecure HTTP is not allowed by platform
    【flutter 溢出BUG】 bottom overflowed by xxx PIXELS
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/10562564.html
Copyright © 2011-2022 走看看