springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected single错误

简介:

在springmvc中注入服务时用@Service

当有两个实现类时都标明@Service后则会出现异常:

nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.james.dao.impl.BaseDao] is defined: expected single matching bean but found 2: 

这是因为都标明了@Service会自动注入,这时会导致不清楚实际运行时实例化哪一类。

解决:

方法一

实际运用哪一个实现类就在哪个类上注入@Service,另外一个不标注

方法二 

都不标注@Service,在resource文件夹中配置 applicationContext.xml,表明实际运行哪个实现类。

<bean id="serviceImpl" class="com.james.service.impl.ServiceImpl"></bean>

本文转自欢醉博客园博客,原文链接http://www.cnblogs.com/zhangs1986/p/7834366.html如需转载请自行联系原作者


欢醉

相关文章
|
10月前
|
Java 数据库连接 mybatis
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
109 0
|
7月前
|
Java 数据库连接 Redis
Bean method ‘redisConnectionFactory‘ not loaded because @ConditionalOnClass did not find required c
Bean method ‘redisConnectionFactory‘ not loaded because @ConditionalOnClass did not find required c
48 0
|
6天前
|
XML 缓存 Java
Spring5源码(7)-lookup-method和replace-method注入
Spring5源码(7)-lookup-method和replace-method注入
30 0
|
9月前
|
Java Apache Spring
解决required a single bean, but 2 were found问题
背景:springboot整合shiro中自定义Realm时出现 错误描述 Parameter 0 of method getDefaultWebSecurityManager in cn.ken.springboot_shiro.config.ShiroConfig required a single bean, but 2 were foun
|
10月前
|
消息中间件 Java RocketMQ
【Java】Error creating bean with name ‘functionBindingRegistrar‘ defined in class path resource的一种解决方式
【Java】Error creating bean with name ‘functionBindingRegistrar‘ defined in class path resource的一种解决方式
59 0
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type &#39;&#39;&#39; in your configuration...
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type &#39;&#39;&#39; in your configuration...
241 0
|
11月前
|
druid
A bean with that name has already been defined in class path resource and overriding is disabled.
A bean with that name has already been defined in class path resource and overriding is disabled.
155 0
|
Nacos
Nacos报错:Error creating bean with name ‘authFilterRegistration‘ defined in class path resource
Nacos报错:Error creating bean with name ‘authFilterRegistration‘ defined in class path resource
371 0
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration...
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration...
153 0