Mohit Bhagat
Mohit Bhagat

Reputation: 253

No qualifying bean of type found for dependency: Spring Boot

I am using a simple spring boot application and I want to connect my database using Hibernate. I am not able to inject my Repository class into my Controller class. Please help. However when I remove the problem causing code, my application is able to scan every component and it works fine. I have created my code through Eclipse.

My starter class

package spring.starter;
@SpringBootApplication
@ComponentScan(basePackages = "spring")
public class Starter {

public static void main(String args[]) {

    ApplicationContext ctx = SpringApplication.run(Starter.class, args);

    }

}

My Controller class

package spring.controller;
@RestController
@RequestMapping("/app")
public class AdminController {

/*@Autowired
private AdminService service;*/

@Autowired
@Qualifier("adminRepository")
private AdminRepository adminRepository;

@GetMapping("/admin")
public List<Admin> getAllAdmin() {
    return adminRepository.findAll();
}
}

My repository class: For this class I have tried removing both annotations but that also dint work.

package spring.repository;
@Repository
@Qualifier("adminRepository")
   public interface AdminRepository extends JpaRepository<Admin, Integer>{

 }

Exception trace

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [spring.repository.AdminRepository] found for dependency [spring.repository.AdminRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=adminRepository)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
... 19 common frames omitted

Upvotes: 2

Views: 6068

Answers (2)

Mohit Bhagat
Mohit Bhagat

Reputation: 253

I found the problem, my starter class was in the package spring.starter all I had to do is to move this class to one level up in the package structure for example:

package spring;

@SpringBootApplication
@ComponentScan(basePackages = "spring")
public class Starter {

    public static void main(String args[]) {

        ApplicationContext ctx = SpringApplication.run(Starter.class, args);

    }

}

And then it worked. But I have a doubt here. What is the benefit of @ComponentScan then ?

Upvotes: 1

dunni
dunni

Reputation: 44555

Spring Data JPA repositories are not detected by the ComponentScan annotation. For them you have to either provide the annotation

@EnableJpaRepositories(basePackages = "spring")

or you change the SpringBootApplication annotation as following:

@SpringBootAnnotation(scanBasePackages = "spring")

This will then cover the custom package configuration for ComponentScan as well as EnableJpaRepositories. After one of those changes, you can also remove @Repository from the repository. That annotation is not meant for Spring Data JPA repository interfaces and has no effect on them.

Upvotes: 1

Related Questions