记录一个错误

通过@Resource注解,将IStateHandler接口的实现类 StateHandlerImpl注入进来

@Resource
private IStateHandler stateHandler;

@Resource注解默认按照名称进行装配,这里抛出异常是因为IStateHandlerStateHandlerImpl都被 Spring 容器管理,在进行 bean 注入时,SpringBoot 不知道注入哪一个 bean

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.itedus.lottery.test.domain.ActivityTest': 
  Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
  No qualifying bean of type 'cn.itedus.lottery.domain.activity.service.stateflow.IStateHandler' available: expected single matching bean but found 2: stateHandlerImpl,IStateHandler

在这里插入图片描述

一下午过去越想越深入😭,最终发现在启动类上标识了@MapperScan注解,并且后面没有加具体的扫描范围,意味着 Spring 容器将会扫描所有包下的接口,并将它们作为映射器进行管理,@MapperScan 是MybatisPlus中的一个注解,用于扫描项目中的Dao层,将 dao 接口类注入到 Spring,能够让其他类进行引用。

@SpringBootApplication
@Configurable
@EnableDubbo
@MapperScan
public class LotteryApplication {

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

}

观察包路径,发现IStateHandlerStateHandlerImpl 确实在 cn.itedus.lottery 包下被扫描交给了 Spring 容器管理
cn.itedus.lottery.LotteryApplication
cn.itedus.lottery.domain.activity.service.stateflow.IStateHandler
cn.itedus.lottery.domain.activity.service.stateflow.impl.StateHandlerImpl

解决:去掉@MapperScan便可以正常注入进来了

补充

@ComponentScan和@MapperScan都是Spring框架中的注解,用于自动扫描并注册Bean。它们的区别如下:

  1. @ComponentScan:用于扫描指定包下的类,并将其注册为Spring容器的Bean。它可以扫描普通组件(如@Service、@Controller、@Component 等)以及自定义注解的组件。默认情况下,它只会扫描当前包及其子包下的类。
  2. @MapperScan:用于扫描指定包下的MyBatis接口,并将其注册为Spring容器的Bean。这样,我们就可以在不使用XML配置文件的情况下,直接使用接口的方式进行MyBatis操作。默认情况下,它只会扫描当前包及其子包下的类。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PJP__00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值