"No qualifying bean of type" for JPA repository in Spring Boot
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.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 documentation on best practices of structuring your code.
Nasreddin
Updated on March 19, 2020Comments
-
Nasreddin about 4 years
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 usedEnableJpaRepositories
to specify the package where JPA repository is defined. Here is what my project looks like://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.
//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 thatSeqServiceBean
. In theSeqServiceBean
I autowired the JPA repository:// 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?