Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No qualifying bean of type" for JPA repository in Spring Boot

Tags:

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?

like image 399
Nasreddin Avatar asked Mar 17 '16 21:03

Nasreddin


People also ask

How do I fix no qualifying bean of type?

The error is “expected at least 1 bean which qualifies as autowire candidate.” The solution is to add the injection class in the ApplicationContext using annotation @Component. The exception “NoSuchBeanDefinitionException: No qualifying bean of type” is resolved if the annotation @Component is added in the Lion class.

What is no Repository bean annotation?

Annotation Type NoRepositoryBean This will typically be used when providing an extended base interface for all repositories in combination with a custom repository base class to implement methods declared in that intermediate interface.

What is the return type of findById in JPA?

Its findById method retrieves an entity by its id. The return value is Optional<T> . Optional<T> is a container object which may or may not contain a non-null value. If a value is present, isPresent returns true and get returns the value.

Why is there no qualifying Bean of type in Spring Boot?

No qualifying bean of type in Spring or Spring Boot. Reason 1: You forgot to declare the bean itself. Reason 2: You have not added package name to @ComponentScan. In this post, we will see about an exception: No qualifying bean of type.

Why is beanb not defined in the current Spring context?

If beans are not automatically scanned but instead defined manually, then BeanB is simply not defined in the current Spring Context. 3. Cause: Field […] in […] Required a Bean of Type […] That Could Not Be Found In a Spring Boot application for the above scenario, we get a different message.

What is JPA repository in Spring Boot?

So, basically, Jpa Repository contains the APIs for basic CRUD operations, the APIS for pagination, and the APIs for sorting. For managing an Office entity with a JPARepository, the following Spring application is used. The following program is mainly a console program, and the data is saved in the H2 database.

Why can’t I add a qualifying bean to componentscan?

Reason 1: You forgot to declare the bean itself. Reason 2: You have not added package name to @ComponentScan. In this post, we will see about an exception: No qualifying bean of type.


1 Answers

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.

like image 60
Ali Dehghani Avatar answered Oct 21 '22 04:10

Ali Dehghani