简体   繁体   English

Spring Boot 1.5.1和Spring Data MongoDB没有适用于存储库的合格Bean

[英]Spring Boot 1.5.1, Spring Data MongoDB No qualifying bean for repository

In my SpringBoot 1.5.1 project I have added following Maven dependency: 在我的SpringBoot 1.5.1项目中,我添加了以下Maven依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

and created a Spring Data MongoDB repository: 并创建了一个Spring Data MongoDB存储库:

package com.example.domain.repository.decision.parameter;

@Repository
public interface CustomerRepository extends MongoRepository<DecisionAnalysisParameter, String> {
}

This is my model: 这是我的模型:

@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;

...
}

Right now I can successfully inject 现在我可以成功注入

@Autowired
private MongoTemplate mongoTemplate;

and operate with a collections through this object. 并通过该对象使用集合进行操作。

But my application fails when I try to inject: 但是,当我尝试注入时,我的应用程序失败:

@Autowired
private CustomerRepository customerRepository;

with a following exception: 除了以下例外:

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

This is my configuration classes: 这是我的配置类:

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

Also, I use Neo4j repositories which are working fine. 另外,我使用的Neo4j存储库运行良好。 How to fix it ? 如何解决?

If you are using multiple Spring Data modules (Neo4J + MongoDB in your case) you need to set a strict configuration for both type of repositories. 如果您使用多个Spring Data模块(在您的情况下为Neo4J + MongoDB),则需要为两种类型的存储库设置严格的配置。 For example: 例如:

@Configuration
@ComponentScan("com.example")
@EnableMongoRepositories(basePackages = "com.example.domain.repositories.mongodb")
@EnableNeo4jRepositories(basePackages = "com.example.domain.repositories.neo4j")
@SpringBootApplication
public class TestConfig {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM