注意不要混用@MapperScan和@ComponentScan

本文讲述了在Spring框架中,@MapperScan和@ComponentScan注解的区别。@ComponentScan用于扫描@Controller、@Service、@Repository等组件,而@MapperScan专门用于扫描Mapper接口。当在main方法中误用@ComponentScan替代@MapperScan时,会导致Bean创建失败,报NoSuchBeanDefinitionException。解决方案是恢复使用@MapperScan注解。了解这两个注解的正确使用对于Spring应用的配置至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、首先@MapperScan和@ComponentScan都是扫描包

2、@ComponentScan是组件扫描注解,用来扫描@Controller  @Service  @Repository这类,主要就是定义扫描的路径从中找出标志了需要装配的类到Spring容器中

3、@MapperScan 是扫描mapper类的注解,就不用在每个mapper类上加@MapperScan了

这两个注解是可以同时使用的。

接下来是我的问题:

main方法使用了 @ComponentScan代替@MapperScan,导致报错。

Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'managerService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.admin.mapper.ManagerUser] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}

解决:只需要改回@MapperScan即可解决

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值