SpringBoot JPA 中无法注入 JpaRepository 接口的问题及解决方案
引入JpaRepository依赖,spring boot只需定义仓库接口,在应用程序启动后,该接口在运行时会自动实现。
但我使用jpa时发生错误,找不到我定义的 Repository接口的实现类。
报错如下:
required a bean named ‘xxx.xxx.xxx’ that could not be found.
原因:
没有扫描到repository接口,从而不能正常注入到spring容器
解决:
在 SpringBootApplication入口添加注释:
@EnableJpaRepositories(basePackages = “xxx.xxx.xxx”) //扫描 @Repository 注解;
补充:
@ComponentScan(basePackages = “xxx.xxx.xxx”) //扫描 @Controller、@Service 注解;
@EntityScan(basePackages = “xxx.xxx.xxx”) //扫描 @Entity 注解;