🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In Spring's Dependency Injection, the NoUniqueBeanDefinitionException is a common exception developers encounter. In this article, we will dive into what this exception is, its primary causes, and how to address them.
What is NoUniqueBeanDefinitionException?
The NoUniqueBeanDefinitionException is thrown when the Spring container finds more than one bean of the same type and is unsure which one to inject. This ambiguity arises in the context of autowiring by type.
Here's a typical error message you might see:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type '[SomeType]' available:
expected single matching bean but found 2: [beanName1, beanName2]How to Reproduce the Issue and Fix this issue
Reproduce: Multiple Beans of the Same Type
public interface Animal {
String sound();
}
@Component
public class Dog implements Animal {
@Override
public String sound() {
return "Woof!";
}
}
@Component
public class Cat implements Animal {
@Override
public String sound() {
return "Meow!";
}
}@Component
public class AnimalService {
private final Animal animal;
@Autowired
public AnimalService(Animal animal) {
this.animal = animal;
}
public void makeSound() {
System.out.println(animal.sound());
}
}org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.springboot.demo.Animal' available:
expected single matching bean but found 2: cat,dogHow to Fix
@Autowired
public AnimalService(@Qualifier("cat") Animal animal) {
this.animal = animal;
}@Component
@Primary
public class Dog implements Animal {
//...
}Other Common Causes & Solutions
Accidental Bean Overriding
Cause: Sometimes, multiple configurations or component scans could lead to beans being unintentionally overridden or duplicated.
Solution: Review your configurations and component scans. Ensure there's no unintended overriding or duplication of bean definitions.
External Libraries with Bean Definitions
Cause: Sometimes, external libraries or modules you include might define beans of their own. If these bean types match with the ones in your application, ambiguity arises.
Solution: Use the @Qualifier annotation to specify the exact bean name you want to inject. If you have control over the library, you can also modify the bean definitions to ensure uniqueness.
Profiles
Cause: Different beans of the same type can be associated with different Spring profiles.
Solution: Ensure that only one of the beans is active for the current profile. If multiple profiles are active, consider restructuring your beans or using the @Qualifier annotation for clarity.
Conclusion
The NoUniqueBeanDefinitionException error in Spring signifies ambiguity in the dependency injection process. Resolving this typically involves being explicit about your intentions, either by specifying which bean you want to use or by ensuring that there's only one suitable candidate in the context. Always remember that clarity in configuration helps keep such issues at bay.
Related Spring Exceptions Posts
- BeanCreationException in Spring Boot
- BeanInstantiationException in Spring Boot
- BeanDefinitionStoreException in Spring
- DataIntegrityViolationException in Spring Boot
- Spring InvalidDataAccessApiUsageException
- NoHandlerFoundException in Spring Boot
- HttpMessageNotReadableException in Spring Boot
- HttpMediaTypeNotSupportedException in Spring Boot
- MethodArgumentNotValidException in Spring Boot
- NoUniqueBeanDefinitionException Spring Boot
- UnsatisfiedDependencyException in Spring Boot
- Unsatisfied Dependency in Spring Boot
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment