gpt4 book ai didi

java - Spring Boot 1.5.1,Spring Data MongoDB 存储库没有合格的 bean

转载 作者:可可西里 更新时间:2023-11-01 10:02:39 24 4
gpt4 key购买 nike

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

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

并创建了一个 Spring Data 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;

...
}

现在我可以成功注入(inject)

@Autowired
private MongoTemplate mongoTemplate;

并通过该对象对集合进行操作。

但是当我尝试注入(inject)时我的应用程序失败了:

@Autowired
private CustomerRepository customerRepository;

有以下异常(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

这是我的配置类:

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

此外,我使用的 Neo4j 存储库运行良好。如何解决?

最佳答案

如果您使用多个 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 {
}

关于java - Spring Boot 1.5.1,Spring Data MongoDB 存储库没有合格的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42254466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com