zoukankan      html  css  js  c++  java
  • Spring学习(10)--- @Qualifier注解

    • 按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以指定单独的构造器参数或方法参数
    • 可用于注解集合类型变量

    例子:

    package com.mypackage;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    public class MovieRecommender {
    
    	@Autowired
    	@Qualifier("main")
    	private MovieCatalog movieCatalog;
    	
    }
    
    package com.mypackage;
    
    import org.springframework.beans.factory.annotation.Qualifier;
    
    public class MovieRecommender {
    
    	private MovieCatalog movieCatalog;
    
    	public void prepare(@Qualifier("main")MovieCatalog movieCatalog){
    		this.movieCatalog=movieCatalog;
    	}
    	
    }
    

    PS:应用于构造器的方法比较常用

    • XML文件中使用qualifier:
    <?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-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd">
             
            <context:component-scan base-package="com.multibean">
            </context:component-scan>   
            
            <bean class="com.mypackage.MovieCatalog">
            	<qualifier value="main"></qualifier>
            </bean>
            
            <bean class="com.mypackage.MovieCatalog">
            	<qualifier value="action"></qualifier>
            </bean>
    </beans>
    
    • 如果通过名字进行注解注入,主要使用的不是@Autowired(即使在技术上能够通过@Qualifier指定bean的名称),替代方式是使用JSR-250@Resource注解,它通过其独特的名称来定义来识别特定的目标(这是一个与所声明的类型是无关的匹配过程)
    • 因语义差异,集合或Map类型的bean无法通过@Autowired来注入,因为没有类型匹配到这样的bean,为这些bean使用@Resource注解,通过唯一名称引用集合或Map的bean
    • @Autowired适用于fields,constructors,multi-argument method这些允许在参数级别使用@Qualifier注解缩小范围的情况
    • @Resource适用于成员变量,只有一个参数的setter方法,所以在目标是构造器或者一个多参数方法时,最好的方式是使用@Qualifier

    例子:

    先定义一个BeanInterface接口

    package com.multibean;
    
    public interface BeanInterface {
    
    }
    

    在定义两个实现类

    package com.multibean;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanInterfaceImpl implements BeanInterface {
    
    }
    

      

    package com.multibean;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanInterface2Impl implements BeanInterface {
    
    }
    

    定义BeanInvoker实现@Qualifier指定bean

    package com.multibean;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanInvoker {
    
    	@Autowired
    	@Qualifier("beanInterfaceImpl")
    	private BeanInterface beanInterface;
    	
    	public void say(){
    		
    		if(null != beanInterface){
    			System.out.println(beanInterface.getClass().getName());
    		}else{
    			System.out.println("BeanInterface is null.");
    		}
    		
    	}
    	
    }
    

    单元测试:

    package com.multibean;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class UnitTest {
    
    	@Test
    	public void test(){
    		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");
    		BeanInvoker beanInvoker = (BeanInvoker)context.getBean("beanInvoker");
    		beanInvoker.say();
    	}
    }
    

    结果:

    七月 06, 2015 11:41:38 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e397bcb: startup date [Mon Jul 06 23:41:38 CST 2015]; root of context hierarchy
    七月 06, 2015 11:41:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
    com.multibean.BeanInterfaceImpl

    修改BeanInvoker

    package com.multibean;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanInvoker {
    
    	@Autowired
    	@Qualifier("beanInterface2Impl")
    	private BeanInterface beanInterface;
    	
    	public void say(){
    		
    		if(null != beanInterface){
    			System.out.println(beanInterface.getClass().getName());
    		}else{
    			System.out.println("BeanInterface is null.");
    		}
    		
    	}
    	
    }
    

    结果:

    七月 06, 2015 11:43:38 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e397bcb: startup date [Mon Jul 06 23:43:38 CST 2015]; root of context hierarchy
    七月 06, 2015 11:43:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
    com.multibean.BeanInterface2Impl
  • 相关阅读:
    cogs 826. [Tyvj Feb11] GF打dota 次短路详细原创讲解! dijkstra
    cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!
    cogs 647. [Youdao2010] 有道搜索框 Trie树 字典树
    cogs 293. [NOI 2000] 单词查找树 Trie树字典树
    P4550 收集邮票
    P1850 换教室 期望dp
    洛谷 UVA11021 Tribles
    P3802 小魔女帕琪 概率与期望
    P3369 【模板】普通平衡树 01Trie树
    P2765 魔术球问题
  • 原文地址:https://www.cnblogs.com/JsonShare/p/4625753.html
Copyright © 2011-2022 走看看