spring boot使用mongodb时,xxxRepository不能Autowired的问题

Posted 太白的技术博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot使用mongodb时,xxxRepository不能Autowired的问题相关的知识,希望对你有一定的参考价值。

默认情况下,当继承MongoRepository的CRUD在@SpringBootApplication的子包下时,xxxRepository是能够自动被扫描和创建代理的。但是如果不在默认路径下,就无法注入了,即使是扫描路径加到了@ComponentScan也一样。

解决方法:

在springboot启动类中添加@EnableMongoRepositories注解,标注mongoRepository类的路径,如下:

@Configuration

@EnableMongoRepositories(basePackages = {"com.hundsun.ta.base.log.mongo"})

public class BaseServiceAppConfig {
    ...其它配置...
}

这样就可以扫描到了。

以上是关于spring boot使用mongodb时,xxxRepository不能Autowired的问题的主要内容,如果未能解决你的问题,请参考以下文章

想使用spring boot将图像导入mongodb

Spring boot:使用 MongoDB 创建用户 [关闭]

基于spring boot的项目中的spring data mongodb配置

[Carefree MongoDB] Spring Boot MongoDB 自动化配置

如何使用 Spring Boot 在 Mongodb 中保存重复项?

Spring Boot Mongodb .yaml 配置