required a bean of type 'xxx' that could not be found.

解決辦法

  • 新增 application.properties 配置 或者 application.yml 配置
  • 注意檢查下配置完成後,是否有警告!

application.properties:

# mapper 映射地址(路徑改成自己的哦)
mybatis.mapperLocations=classpath:/com/example/springbootdemo/mapper/*.xml
mybatis.type-aliases-package=com.example.domain

application.yml:

# 配置 Mybatis.xml 位置
mybatis:
  mapper-locations: classpath:/com/example/springbootdemo/mapper/*.xml
  type-aliases-package: com.example.domain

錯誤原因:xxMapper 注入失敗!

報錯截圖:
在這裏插入圖片描述
驗證:是否是 Mapper 注入的問題(未添加上述配置文件時)

  • @Autowired 後面追加 (required = false): 表示支持 null
  • 運行,然後拋出 空指針異常

在這裏插入圖片描述

required 屬性

  • @Autowired(required=true):當使用 @Autowired 註解的時候,其實默認就是 @Autowired(required=true),表示注入的時候,該 bean必須存在,否則就會注入失敗。
  • @Autowired(required=false):表示忽略當前要注入的 bean,如果有,直接注入;沒有跳過,不會報錯。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章