spring注入属性时@Qualifier不起作用

qq_17072973 qq_17072973 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
刚学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注解没起作用吧,不知是哪里出了问题,麻烦给看看哪有问题,感激不尽!
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
qq_17072973
Java EE 67,535 成员 225,843 内容