Spring Boot 1.5.1,Spring Data MongoDB 存储库没有合格的 bean
Spring Boot 1.5.1, Spring Data MongoDB No qualifying bean for repository
在我的 SpringBoot 1.5.1 项目中,我添加了以下 Maven 依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
并创建了一个 Spring 数据 MongoDB 存储库:
package com.example.domain.repository.decision.parameter;
@Repository
public interface CustomerRepository extends MongoRepository<DecisionAnalysisParameter, String> {
}
这是我的模型:
@Document(collection = "decision_analysis_parameter")
public class DecisionAnalysisParameter implements Serializable {
private static final long serialVersionUID = 1493180175756424789L;
@Id
private String id;
@NotNull
private Long decisionId;
private Set<BaseQuery> characteristicFilterQueries;
private Set<Long> sortCriteriaIds;
private Direction sortWeightCriteriaDirection;
private Direction sortTotalVotesCriteriaDirection;
private Map<String, Double> sortCriteriaCoefficients;
private Long sortCharacteristicId;
private Direction sortCharacteristicDirection;
private String sortDecisionPropertyName;
private Direction sortDecisionPropertyDirection;
private Set<Long> excludeChildDecisionIds;
private Set<Long> includeChildDecisionIds;
private Long userId;
private Integer pageNumber;
private Integer pageSize;
...
}
现在我可以成功注入
@Autowired
private MongoTemplate mongoTemplate;
并通过此对象对集合进行操作。
但是当我尝试注入时我的应用程序失败了:
@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 {
}
在我的 SpringBoot 1.5.1 项目中,我添加了以下 Maven 依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
并创建了一个 Spring 数据 MongoDB 存储库:
package com.example.domain.repository.decision.parameter;
@Repository
public interface CustomerRepository extends MongoRepository<DecisionAnalysisParameter, String> {
}
这是我的模型:
@Document(collection = "decision_analysis_parameter")
public class DecisionAnalysisParameter implements Serializable {
private static final long serialVersionUID = 1493180175756424789L;
@Id
private String id;
@NotNull
private Long decisionId;
private Set<BaseQuery> characteristicFilterQueries;
private Set<Long> sortCriteriaIds;
private Direction sortWeightCriteriaDirection;
private Direction sortTotalVotesCriteriaDirection;
private Map<String, Double> sortCriteriaCoefficients;
private Long sortCharacteristicId;
private Direction sortCharacteristicDirection;
private String sortDecisionPropertyName;
private Direction sortDecisionPropertyDirection;
private Set<Long> excludeChildDecisionIds;
private Set<Long> includeChildDecisionIds;
private Long userId;
private Integer pageNumber;
private Integer pageSize;
...
}
现在我可以成功注入
@Autowired
private MongoTemplate mongoTemplate;
并通过此对象对集合进行操作。
但是当我尝试注入时我的应用程序失败了:
@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 {
}