Spring学习(10)--- @Qualifier注解

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值