45

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:

enter image description here

//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 that SeqServiceBean. In the SeqServiceBean 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?

8
  • It basically says, it can't find the eligible SeqRepository instance to autowire. Add your project structure and some repository related code. Commented Mar 17, 2016 at 22:35
  • refactor Application.java annotations delete all use this @SpringBootApplication @EntityScan({"org.mdacc.rists.cghub.model"}) @EnableJpaRepositories(basePackages = {"org.mdacc.rists.cghub.ws.repository"}) Commented Mar 17, 2016 at 23:30
  • @kakashi hatake tried what you suggested and still the same error Commented Mar 18, 2016 at 13:56
  • @AliDehghani Added project structure and repository class. Commented Mar 18, 2016 at 13:59
  • Can you move your Application into org.mda package and delete all the EntityScan and EnableJpaRepositories? Commented Mar 18, 2016 at 14:03

1 Answer 1

59

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Where should this annotation be added? Repository class? WebApplicationInitializerClass? Something else
why should we add this annotation? I thought spring will scan the packages by itself!
If you do not add this annotation, Spring framework will not create a bean of the interface where you use @Repository annotation above. Spring Boot handles this internally for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.