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

qq_17072973 2017-09-02 10:29:03
刚学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注解没起作用吧,不知是哪里出了问题,麻烦给看看哪有问题,感激不尽!
...全文
947 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qvp8 2017-11-09
  • 打赏
  • 举报
回复 1
factory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
a13979053726 2017-10-29
  • 打赏
  • 举报
回复
是哪里的网址出错了?我也遇到了这个问题。
qq_17072973 2017-09-07
  • 打赏
  • 举报
回复
引用 1 楼 shansongjia 的回复:
[quote=引用 楼主 qq_17072973 的回复:] 刚学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注解没起作用吧,不知是哪里出了问题,麻烦给看看哪有问题,感激不尽!
你可以看一下这个http://www.cnblogs.com/smileLuckBoy/p/5801678.html[/quote] 谢啦,问题解决了,是配置文件上边那一堆网址的问题
Buguanjia-Ssj 2017-09-05
  • 打赏
  • 举报
回复
引用 楼主 qq_17072973 的回复:
刚学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注解没起作用吧,不知是哪里出了问题,麻烦给看看哪有问题,感激不尽!
你可以看一下这个http://www.cnblogs.com/smileLuckBoy/p/5801678.html

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧