Saturday, May 4, 2024

How to fix @Autowired - No qualifying bean of type found for dependency in Spring Boot? [Solved]

Hello guys, if you are working in a Java and Spring Boot project and you are getting errors like "@Autowired - No qualifying bean of the type found for dependency " then you have come to the right place. Earlier, I have shared how to solve error creating bean with name in Spring framework and In this tutorial, we are going to discuss how to fix @Autowired - No qualifying bean of the type found for dependency. When we come across the org.springframework.beans.factory, we need to be careful. Whether there isn't a qualifying bean of type XXXX accessible, we should see if there is a spring bean called XXXX in the runtime. In most cases, we simply try to inject a bean that isn't defined.

In the spring framework, there are three ways to configure beans as follows,
1. Annotation-based configuration - Using @Service or @Component annotations, scope details can be provided with @Scope annotation.
2. XML-based annotation - The XML-based configuration can be loaded automatically by writing some boilerplate code in the web.xml file.
3. Java-based configuration - We can configure spring beans using java programs. Important annotations used in there are @Configuration, @ComponentScan, and @Bean.

We are getting an example like below, Classroom needs to add the Student collaborator. Assume that the Student bean is not defined yet.
@Component
public class ClassRoom {

@Autowired
private Student studentDependency;
}

So after running this code, we get an error which is having the following,

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.studentexample.model.Student] 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)}

So why does this error come, Normally the beans are scanned by classpath scanning and they should be correctly annotated with @Service, @Component, @Controller, or @Repository.

@Configuration
@ComponentScan("com.studentexample.Student")
public class JavaConfig {

}
If you defined the beans manually, the Student bean will not define in the current spring context.



1. No Qualifying bean of type [] is defined.

So another exception that we get is, having two beans definitions in the context. As an example, if you have an interface Student which is implemented from both the ClassA and ClassB
@Component
public class ClassA implements Student {

}
@Component
public class ClassB implements Student {

}

Think, if the ClassA autowired the Student interface first, then the Spring is not able to know which one to inject.


@Component
public class ClassA {

@Autowired
private Student studentDependency;
}

This again, result in the NoSuchBeanDefinitionException being thrown by the BeanFactory gives the following error.


    Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [com.studentexample.model.Student] is defined:
expected single matching bean but found 2: ClassA,ClassB

This is clearly mentioned that the, "expected single matching bean but found 2". The actual exception being produced in this situation, however, is not NoSuchBeanDefinitionException, but a subtype - the NoUniqueBeanDefinitionException

This new exception was added in Spring 3.2.1 to distinguish between the case where no bean definition was discovered and the case where several definitions were found in the context.

So one of the solutions for this is using the @Qualifier annotation in your spring application. This will help you to bind up the exact bean that you want to bind.

 
@Component
public class ClassA {

@Autowired
@Qualifier("Student")
private Student studentDependency;
}

So in here, the spring context will be able to decide whether inject ClassA or ClassB.





2. No Bean Named[..] is defined.

If you requested a bean that is not defined from a name, this also leads to throwing the NoSuchBeanDefinitionException


@Component
public class ClassA implements InitializingBean {

@Autowired
private ApplicationContext context;

@Override
public void afterPropertiesSet() {
context.getBean("student");
}
}

Think, there is no bean with the name of the student and this will lead to having the following error.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'student' is defined






3. Proxied beans

In the Spring transactional support, specifically beans annotated with @Transactional – is a typical reason for the bean being proxied. As an example, if you have the services which both are transactional and injecting by the class definition will not work.


@Service
@Transactional
public class ClassA implements IStudentA{

@Autowired
private StudnetServiceB studentServiceB;

}

@Service
@Transactional
public class ClassB implements IStudentB{

@Autowired
private StudnetServiceA studnetServiceA;
}


How to fix @Autowired - No qualifying bean of type found for dependency in Spring Boot

So this will leads to an error and we can correct the error using following the method in below.

@Service
@Transactional
public class ClassA implements IStudentA{

@Autowired
private IStudentB serviceB;

}

@Service
@Transactional
public class ClassB implements IStudentB{

}

So in this tutorial, we discussed how to fix @Autowired - No qualifying bean of the type found for dependency with many examples. As discussed above, this is a very important concept and needs to have a proper idea before going into complex problems in spring boot. Hope you understand this tutorial and see you in the next tutorial.

Other Java and Spring Tutorial you may like
  
  • How Spring MVC works internally? (answer)
  • Spring Data JPA Repository (JpaReposistory example)
  • Difference between @RequestParam and @PathVariable in Spring (answer)
  • Top 7  Courses to learn Microservices in Java (courses)
  • Difference between @Autowired and @Inject in Spring? (answer)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • How to create your first Spring MVC application (tutorial)
  • How to update an entity using Spring Data JPA? (example)
  • How to upload and download file using Spring Boot (Example)
  • 10 Spring MVC annotations Java developer should know (annotations)
  • What is @Conditional annotation in Spring? (conditional example)
  • 20+ Spring MVC Interview Questions for Programmers (answer)
  • 13 Spring Boot Actuator Interview questions (boot questions)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • 5 Courses to Learn Spring Security for Java programmers (courses)
  • Top 5 Spring Boot Annotations Java Developers should know (read)
  • Spring Boot + Reactjs example (example)
  • 5 Spring Cloud annotations Java programmer should learn (cloud)
  • Spring Data JPA @Query Example (query example)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • @SpringBootApplication vs. @EnableAutoConfiguration? (answer)

Thanks for reading this article so far. If you find this solution of No qualifying bean of type found for dependency in spring framework and spring boot applications  then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you are a beginner and want to learn the Framework from scratch and look for some of the best online resources, you can also check out these best Spring MVC courses for beginners. This list contains free Udemy and Pluralsight courses to learn Spring MVC from scratch.      

1 comment :

Anonymous said...

thanks is helps a lot.

Post a Comment