Springboot中,serviceImpl對Dao層Autowired一直為NULL


  搭建框架時,serviceImpl對Dao層Autowired一直為NULL。

  Dao層注解為@Repository,serviceImpl注解為@Component,Controller層注解為@RestController。

調用順序為在Controller中,實例化serviceImpl,然后在serviceImpl中對Dao進行@Autowired注入。

我傻了,很明顯的錯誤,不應該在Controller中,對serviceImpl實例化,應該是@Autowired,但是serviceImpl不能被Autowired。

所以需要添加一個Service,注解為@Service(自己之前忘了加)。

然后正確順序是,在Controller中,對Service進行@Autowired(這樣IDEA被報錯,但是沒有影響!),然后ServiceImpl實現Service接口中的方法,然后在serviceImpl中對Dao進行@Autowired注入!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM