• 當前位置:首頁 > IT技術 > 編程語言 > 正文

    項目啟動報錯怎么辦?看看你Spring自動注入用對了嘛?@Autowired XxxService注入問題解決
    2022-01-01 23:12:32

    問題

    • 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容器注入規則

    本文摘自 :https://www.cnblogs.com/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码