【问题标题】:"No qualifying bean of type" for JPA repository in Spring BootSpring Boot 中 JPA 存储库的“没有限定类型的 bean”
【发布时间】:2016-07-04 12:04:37
【问题描述】:

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

//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 有什么关系?

【问题讨论】:

  • 它基本上是说,它找不到符合条件的SeqRepository 实例来自动装配。添加您的项目结构和一些与存储库相关的代码。
  • 重构 Application.java 注释删除所有使用这个 @SpringBootApplication @EntityScan({"org.mdacc.rists.cghub.model"}) @EnableJpaRepositories(basePackages = {"org.mdacc.rists.cghub .ws.repository"})
  • @kakashi hatake 尝试了你的建议,仍然是同样的错误
  • @AliDehghani 添加了项目结构和存储库类。
  • 你能不能把你的Application移动到org.mda包里并删除所有EntityScanEnableJpaRepositories

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


【解决方案1】:

您扫描了 EnableJpaRepositories 中的错误包。没有org.mdacc.rists.cghub.ws.repository 包。所以,改用这个:

@EnableJpaRepositories("org.mda.rists.cghub.ws.repository") 

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

【讨论】:

  • 这个注解应该加在哪里?存储类? WebApplicationInitializerClass?别的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
相关资源
最近更新 更多