Java Spring Boot 中 JPA 存储库的“没有合格的 bean 类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36072445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"No qualifying bean of type" for JPA repository in Spring Boot
提问by Nasreddin
I am implementing Rest API with Spring Boot. Since my entity classes are from a package from another package, I had to specify that with annotation EntityScan
. Also, I used EnableJpaRepositories
to specify the package where JPA repository is defined. Here is what my project looks like:
我正在使用 Spring Boot 实现 Rest API。由于我的实体类来自另一个包的包,因此我必须使用 annotation 指定它EntityScan
。此外,我曾经EnableJpaRepositories
指定定义 JPA 存储库的包。这是我的项目的样子:
//Application.java
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EntityScan("org.mdacc.rists.cghub.model")
@EnableJpaRepositories("org.mdacc.rists.cghub.ws.repository")
In my controller class I had a SeqService object autowired.
在我的控制器类中,我自动装配了一个 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
is an interface from which I created a Bean class for that SeqServiceBean
. In the SeqServiceBean
I autowired the JPA repository:
SeqService
是一个接口,我从中创建了一个 Bean 类SeqServiceBean
。在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);
}
However the application couldn't start due to the following error:
但是,由于以下错误,应用程序无法启动:
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]
I don't understand the error. What does it have to do with qualifying bean?
我不明白这个错误。它和合格的 bean 有什么关系?
采纳答案by Ali Dehghani
You were scanning the wrong package in your EnableJpaRepositories
. There is no org.mdacc.rists.cghub.ws.repository
package. So, use this instead:
您扫描了错误的EnableJpaRepositories
. 没有org.mdacc.rists.cghub.ws.repository
包。所以,改用这个:
@EnableJpaRepositories("org.mda.rists.cghub.ws.repository")
Spring Boot does not require any specific code layout to work, however, there are some best practices that will help you. Check out the spring boot documentationon best practices of structuring your code.
Spring Boot 不需要任何特定的代码布局即可工作,但是,有一些最佳实践可以帮助您。查看有关构建代码的最佳实践的Spring Boot 文档。