spring注入属性时@Qualifier不起作用
刚学spring就遇到一个奇怪的问题,在网上找了半天也没解决。。这是我的demo,一个接口两个bean实现,再选择注入到含有该接口的service,代码如下:
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean id="springHello" class="com.spring.service.impl.SpringHello"></bean>
<bean id="mybatisHello" class="com.spring.service.impl.MybatisHello"></bean>
<bean id="helloService" class="com.spring.service.HelloService"></bean>
</beans>
public class HelloService
{
// ??? @Qualifier不起作用
@Autowired
@Qualifier("springHello")
Hello hello;
public HelloService()
{
}
//@Autowired
public HelloService(Hello hello){
this.hello = hello;
System.out.println("auto-write by constructor");
}
public Hello getHello()
{
return hello;
}
//@Autowired
public void setHello(Hello hello)
{
System.out.println("auto-write by type");
this.hello = hello;
}
}
如上,运行时报错
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.wjm.spring.service.Hello' available: expected single matching bean but found 2: springHello,mybatisHello显然是@Qualifier注解没起作用吧,不知是哪里出了问题,麻烦给看看哪有问题,感激不尽!
...全文
相关推荐
Spring中的自动装配有哪些限制?.docx
Spring中的自动装配有哪些限制?.docx Spring中的自动装配有哪些限制?.docx Spring中的自动装配有哪些限制?.docx
Spring @Primary和@Qualifier注解原理解析
主要介绍了Spring @Primary和@Qualifier注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
关于spring boot中几种注入方法的一些个人看法
主要给大家介绍了关于spring boot中几种注入方法的一些个人看法,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
详解Spring中接口的bean是如何注入的
主要介绍了详解Spring中接口的bean是如何注入的的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
详解Spring通过@Value注解注入属性的几种方式
本篇文章主要介绍了详解Spring通过@Value注解注入属性的几种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。


