Mybatis 遇到的坑 required a single b

2020-10-25  本文已影响0人  吉他手_c156

Description:

Field accountService in com.seata.account.dubbo.AccountDubboServiceImpl required a single bean, but 2 were found:

- TAccountServiceImpl: defined in file [G:\gu-pao\Mick-Distributed-Concurrency\alibaba\seata\springboot-dubbo-seata-integration\springboot-dubbo-seata-account\target\classes\com\seata\account\service\impl\TAccountServiceImpl.class]

- ITAccountService: defined in file [G:\gu-pao\Mick-Distributed-Concurrency\alibaba\seata\springboot-dubbo-seata-integration\springboot-dubbo-seata-account\target\classes\com\seata\account\service\ITAccountService.class]

报错信息说  AccountDubboServiceImpl 类中使用 @Autowired 注入了一个 ITAccountService 接口的实现类,这个接口竟然有连个实现类???有没有搞错,我实现了一个接口好不不好,最后打印 spring beanDefinitionNames 发现确实是有连个实现类,这是怎么回事呢?我明明只有一个实现类

原来在使用 @MapperScan 扫描 mapper  接口,指定报名的时候没有告诉 mabatis 扫描包的具体路径,

这时 mabits 会把这个包的的接口以及子包的接口通通生成一个代理类注入都 IOC 容器中,key 就是接口的名字,而真正的实现类也会被装载到 IOC 容器中,实现类首字母小写就是 IOC 的 key ,这时就有两个实现类了,通过 @Autowired 注入的 Bean 就不知道找那个 bean 了,是不是很坑?修改 @MapperScan 包扫描路径精确到具体 mapper 包问题解决

或者使用另一种方法,在 mapper 接口上加上 @Mapper 注解,不使用 @MapperScan 注解

上一篇 下一篇

猜你喜欢

热点阅读