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:
//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?
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With