"没有符合条件的bean#34;对于Spring Boot中的JPA存储库

时间:2016-03-17 21:58:38

标签: java spring jpa spring-boot spring-data

我正在使用Spring Boot实现Rest API。由于我的实体类来自另一个包的包,因此我必须使用注释EntityScan来指定它。此外,我使用EnableJpaRepositories来指定定义JPA存储库的包。这是我的项目的样子:

enter image description here

//Application.java

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EntityScan("org.mdacc.rists.cghub.model")
@EnableJpaRepositories("org.mdacc.rists.cghub.ws.repository") 

在我的控制器类中,我有一个自动装配的SeqService对象。

//SeqController.java

@Autowired private SeqService seqService;

@RequestMapping(value = "/api/seqs", method = GET, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<List<SeqTb>> getSeqs() {
    List<SeqTb> seqs = seqService.findAll();
    return new ResponseEntity<List<SeqTb>>(seqs, HttpStatus.OK);
}

SeqService是一个接口,我从该接口为SeqServiceBean创建了一个Bean类。在SeqServiceBean我自动安装了JPA存储库:

// SeqServiceBean.java

@Autowired private SeqRepository seqRepository;

@Override
public List<SeqTb> findAll() {
    List<SeqTb> seqs = seqRepository.findAll();
    return seqs;
}

//SeqRepository.java

@Repository
public interface SeqRepository extends JpaRepository<SeqTb, Integer> {

    @Override
    public List<SeqTb> findAll();

    public SeqTb findByAnalysisId(String analysisId);
}

但是,由于以下错误,应用程序无法启动:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.mda.rists.cghub.ws.repository.SeqRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]

我不理解错误。它与限定bean有什么关系?

1 个答案:

答案 0 :(得分:13)

您正在Name|Firstname| Vertongen|Marc| 中扫描错误的包裹。没有EnableJpaRepositories个包。所以,请改用它:

org.mdacc.rists.cghub.ws.repository

Spring Boot不需要任何特定的代码布局,但是,有一些最佳实践可以帮助您。查看有关构建代码的最佳做法的spring boot documentation