“No qualifying bean of type found for dependency” is a common error message that developers may encounter when working with the Spring framework. This error occurs when a Spring application is unable to find a bean that satisfies a particular type of dependency. In this blog post, we will take a closer look at this error, the causes behind it, and how to resolve it.
Root Cause
No qualifying bean of type found for dependency is a common error message that occurs when a Spring application is unable to find a bean that satisfies a particular type of dependency.
This error can occur for a variety of reasons, but it is typically caused by one of the following issues:
- The bean in question has not been defined in the application context.
- The bean has been defined, but it is not of the correct type.
- Multiple beans of the same type have been defined, and Spring is unable to determine which one to use.
How to reproduce the error
Here’s an example of a simple Spring application that demonstrates the “No qualifying bean of type found for dependency” error:
@Service
public class MyService {
private final MyDependency myDependency;
public MyService(MyDependency myDependency) {
this.myDependency = myDependency;
}
public void doSomething() {
myDependency.doSomething();
}
}
@Component
public class MyDependency {
public void doSomething() {
// implementation here
}
}
The Exception
In this example, the MyService
class has a constructor that takes a MyDependency
object as a parameter. However, if there is no MyDependency
bean defined in the application context, the following error will occur when the application is started:
Error creating bean with name 'myService':
Unsatisfied dependency expressed through field 'myDependency';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.example.MyDependency' available:
expected at least 1 bean which qualifies as autowire candidate.
Solution 1
To fix this error, you will need to define a MyDependency
bean in the application context. This can be done in several ways:
- Use the
@Component
annotation on theMyDependency
class, which tells Spring to create a bean of that type. - Use the
@Bean
annotation on a method in a@Configuration
class, which returns an instance of theMyDependency
class. - Use the
@Autowired
annotation on the constructor of theMyService
class, which tells Spring to automatically wire theMyDependency
bean to the constructor.
Here’s an example of how to fix the error using the @Component
annotation:
@Service
public class MyService {
private final MyDependency myDependency;
public MyService(MyDependency myDependency) {
this.myDependency = myDependency;
}
public void doSomething() {
myDependency.doSomething();
}
}
@Component
public class MyDependency {
public void doSomething() {
// implementation here
}
}
By adding the @Component
annotation to the MyDependency
class, Spring will automatically create a bean of that type and wire it to the MyService
class’s constructor.
Solution 2
Another case could be that you have multiple beans of the same type and Spring is unable to determine which one to use.In this case, you can use the @Qualifier("beanName")
annotation to specify which bean to use. Here’s an example:
@Service
public class MyService {
private final MyDependency myDependency;
public MyService(@Qualifier("specificMyDependency") MyDependency myDependency) {
this.myDependency = myDependency;
}
public void doSomething() {
myDependency.doSomething();
}
}
@Component
@Qualifier("specificMyDependency")
public class MyDependency {
public void doSomething() {
// implementation here
}
}
@Component
public class AnotherMyDependency {
public void doSomething() {
// implementation here
}
}
In this example, we have two beans of the same type, MyDependency
and AnotherMyDependency
. We use the @Qualifier("specificMyDependency")
annotation on the constructor of the MyService
class to specify that we want to use the MyDependency
bean, and the @Qualifier("specificMyDependency")
annotation on the MyDependency
class to indicate that it is the bean we want to use.
Conclusion
In conclusion, the “No qualifying bean of type found for dependency” error occurs when a Spring application is unable to find a bean that satisfies a particular type of dependency. It can be caused by a variety of reasons such as bean not being defined, bean not of correct type, or multiple beans of the same type. By understanding the causes and using the appropriate annotations, this error can be easily resolved.