解决Spring 启动报错:No qualifying bean of type

问题:

启动Spring boot 报错:

No qualifying bean of type [StatsJobTaskService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

场景:

1.主web项目stats-job 依赖 子项目 stats-service

2.在主项目自动注入一个service

@Autowired private StatsJobTaskService statsJobTaskService;

原因1:主web项目在扫描的时候没有扫描到子项目

方案1:设置扫描路径包含子项目

<context:component-scan base-package="com..*"/>

原因2:另一个子项目也有一个相同的StatsJobTaskService.java,导致spring在自动注入的时候不知道注入哪个

方案1:修改StatsJobTaskService.java类名,不允许两个相同的类名的文件存在

方案2:修改注入方式,使用@Resource(name="statsJobTaskServiceImpl2")方式注入

并且在声明@service是加上名字@Service("statsJobTaskServiceImpl2")

经验分享 程序员 微信小程序 职场和发展