飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • AngularJS教程
  • UEditor使用文档
  • ThinkPHP5.0教程

项目启动报错怎么办?看看你Spring自动注入用对了嘛?@Autowired XxxService注入问题解决

时间:2022-01-02  作者:chova  

问题

  • 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实现类使用如下注解
@域名域名域名ice
@域名域名ice
  • 注意: 要将Service实现类注入到容器,而不是Service接口

总结

  • Spring容器注入规则

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。