启动报错:Field userService in com.zyr.springboot.sys.XXXController required a single bean, but 2 were f

报错信息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userService in com.zyr.springbootdemo.sys.LoginController required a single bean, but 2 were found:
	- sysUserServiceImpl: defined in file [F:\workspace\springboot-demo-mp\target\classes\com\zyr\springbootdemo\sys\user\service\impl\SysUserServiceImpl.class]
	- sysUserService: defined in file [F:\workspace\springboot-demo-mp\target\classes\com\zyr\springbootdemo\sys\user\service\SysUserService.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Disconnected from the target VM, address: '127.0.0.1:50631', transport: 'socket'

Process finished with exit code 1

当在使用spring boot开发的过程中,通常需要实现control层,service层,dao层。其中service层的实现使用@service注解,dao层的实现使用@mapper或者@mapperscan注解。当调用时使用@Autowired注解。
当我们使用 接口 + 实现类的方式实现service层时,只需要在实现类上增加@service注解,正常情况下spring boot的bean容易只应该保存一个该类型的bean实例。在control层可以进行一下调用。
但在使用@mapperscan注解(不指定扫描包)的情况下会出现上面的异常。
原因是由于使用的是@mapperscan注解的问题。

@SpringBootApplication
@MapperScan("com.zyr.springbootdemo")
public class SpringbootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }
}

当启动springboot时启动时,由于存在@MapperScan注解,因此spring boot会将所有的接口(interface)生成一个bean实列保存到容器中。

解决方法有一下几种:
1、注入的变量名写清楚,SysUserService 变量名不能写成userService,这样就会找到两个,接口类和实体类
2.放弃使用@MapperScan
3.指定@MapperScan的扫描路径

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值