springboot 工程啟動報錯之Consider defining a bean of type ‘XXX’ in your configuration.
一、前言:
使用springboot自動注入的方式搭建好了工程,結果啟動的時候報錯了!!!,錯誤如下圖:
Description: Field userEntityMapper in com.xxx.xxx.service.UserService required a bean of type 'com.xxx.xxx.dao.UserEntityMapper' that could not be found. Action: Consider defining a bean of type 'com.xxx.xxx.dao.UserEntityMapper' in your configuration.
二、解決方式:
1.檢查自己寫的注解是否錯了,並沒有。
2.在網上查找解決方式:如下所示:
步驟一:
在springboot的配置文件添加,mybatis的配置如下所示:
mybatis: typeAliasesPackage: com.xxx.xxx.dao.entity mapperLocations: classpath:mapper/*.xml
步驟二:
①將接口與對應的實現類放在與application啟動類的同一個目錄或者他的子目錄下,這樣注解可以被掃描到,這是最省事的辦法。(沒測試)
②或者在啟動類上加上@MapperScan或者@ComponentScan注解,手動指定application類要掃描哪些包下的注解,如下所示:
@SpringBootApplication @ComponentScan(basePackages = {"com.xxx.xxx.dao"})
③或者在接口上添加@Mapper注解。
@Mapper public interface UserMapper { }
ps:之所以沒有找到對應的Bean是因為,@SpringBootApplication沒有掃描到。