关于spring:项目启动报错怎么办看看你Spring自动注入用对了嘛Autowired-XxxService注入问题解决

9次阅读

共计 668 个字符,预计需要花费 2 分钟才能阅读完成。

问题

  • 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 容器注入规定

正文完
 0