Spring boot中使用MongoDB找不到Repository的一种情况及解决办法

Repository的定义:

public interface PersonRepository extends MongoRepository<Person,Long> {
}

启动类中已配置:

@SpringBootApplication(scanBasePackages = {
        "com.xxx.repository"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(FirstdemoApplication.class, args);
    }
}

 

报错内容:

Consider defining a bean of type 'com.xxx.PersonRepository' in your configuration.

 

解决办法:

在启动类添加@EnableMongoRepositories(basePackages = {"com.xxx.repository"}),原有的

@SpringBootApplication种对"com.xxx.repository"的扫描配置也不可少。

最终配置如下:
@SpringBootApplication(scanBasePackages = {
        "com.xxx.repository"})
@EnableMongoRepositories(basePackages = "com.xxx.repository")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(FirstdemoApplication.class, args);
    }
}

 

posted on 2019-03-11 11:02  Hexy  阅读(2195)  评论(1)    收藏  举报

导航