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
,如果有,直接注入;没有跳过,不会报错。