Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot 1.5.1,Spring Data MongoDB没有符合存储库要求的bean_Java_Spring_Mongodb_Spring Boot_Spring Data Mongodb - Fatal编程技术网

Java Spring Boot 1.5.1,Spring Data MongoDB没有符合存储库要求的bean

Java Spring Boot 1.5.1,Spring Data MongoDB没有符合存储库要求的bean,java,spring,mongodb,spring-boot,spring-data-mongodb,Java,Spring,Mongodb,Spring Boot,Spring Data Mongodb,在我的SpringBoot 1.5.1项目中,我添加了以下Maven依赖项: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> 并通过此对象使用集合进行操作 但当我尝试注入时,我的应用程序失败: @Au

在我的SpringBoot 1.5.1项目中,我添加了以下Maven依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
并通过此对象使用集合进行操作

但当我尝试注入时,我的应用程序失败:

@Autowired
private CustomerRepository customerRepository;
除以下例外:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.CustomerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 56 common frames omitted
这是我的配置类:

@Configuration
@ComponentScan("com.example")
@EnableMongoRepositories(basePackages = "com.example.domain.repository")
@SpringBootApplication
public class TestConfig {
}

此外,我使用的Neo4j存储库工作正常。如何修复它?

如果您使用多个Spring数据模块(在您的例子中是Neo4J+MongoDB),则需要为这两种类型的存储库设置严格的配置。例如:

@Configuration
@ComponentScan("com.example")
@EnableMongoRepositories(basePackages = "com.example.domain.repositories.mongodb")
@EnableNeo4jRepositories(basePackages = "com.example.domain.repositories.neo4j")
@SpringBootApplication
public class TestConfig {
}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.CustomerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 56 common frames omitted
@Configuration
@ComponentScan("com.example")
@EnableMongoRepositories(basePackages = "com.example.domain.repository")
@SpringBootApplication
public class TestConfig {
}
@Configuration
@ComponentScan("com.example")
@EnableMongoRepositories(basePackages = "com.example.domain.repositories.mongodb")
@EnableNeo4jRepositories(basePackages = "com.example.domain.repositories.neo4j")
@SpringBootApplication
public class TestConfig {
}