简体   繁体   中英

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:

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

and created a Spring Data MongoDB repository:

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. 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. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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