项目启动报错怎么办?看看你Spring自动注入用对了嘛?@Autowired XxxService注入问题解决
2022/1/1 23:11:34
本文主要是介绍项目启动报错怎么办?看看你Spring自动注入用对了嘛?@Autowired XxxService注入问题解决,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
问题
- 在Controller层使用 @Autowired注入Service时,提示Bean中没有Service
- 在Service接口中使用 @Component注入后,启动项目问题提示:
The web application [ROOT] appears to have started a thread named [DubboClientReconnectTimer-thread-2] but has failed to stop it. This is very likely to create a memory leak
原因
- 提示Bean中没有Service:
- 因为没有将Service注入到Spring容器中,可以通过 @Component或者 @Service注解注入
- 在Service中使用注解注入到容器后,启动项目会报错:
- 因为在Service接口注入,同时注入了两个Bean
- 在Service接口注入,会将实现Service接口的子类也注入到容器中,所以会导致Dubbo重复性线程错误
解决办法
- 在Service的实现类ServiceImpl上,使用 @Component或者 @Service注解将Service注入到Spring容器中
- 如果是使用Dubbo的SpringBoot项目,可以在Service实现类使用如下注解
@com.alibaba.dubbo.config.annotation.Service @org.springframework.stereotype.Service
- 注意: 要将Service实现类注入到容器,而不是Service接口
总结
- Spring容器注入规则
这篇关于项目启动报错怎么办?看看你Spring自动注入用对了嘛?@Autowired XxxService注入问题解决的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求