2017-02-15 75 views
0

在我SpringBoot 1.5.1项目中,我已经添加以下Maven的依赖:春天引导1.5.1,春数据的MongoDB没有合格豆的仓库

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

,并创造了一个春天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存储库工作正常。如何解决它?

回答

1

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

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

看起来像你的CustomerRepository不在正确的包装中。目前它的包装为com.example

@EnableMongoRepositories(basePackages = "com.example.domain.repository") 

移动存储库以匹配basePackagesEnableMongoRepositories注释或更新注释basePackage值到CustomerRepository包。