Service层注解@Service失败原因及解决办法

直接上报错代码
Error creating bean with name 'com.mp.mybatis.MybatisApplicationTests': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mp.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
报错原因分析:

1、Service层没有添加@Service注解

2、有@Service仍然报错

解决办法:

在SpringBoot的启动类@SpringBootApplication上添加scanBasePackages = {“com.mp.service”},扫描Service层

@SpringBootApplication(scanBasePackages = {"com.mp.service"})
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐