This is a common exception thrown by the BeanFactory when the user tries to resolve a bean that simply is not defined in the spring context.
Here we will discuss some possible reasons;
Found for dependency:
The most common reason for this exception is simply trying to inject a bean that is not defined.
Example:
For example, BeanB is wiring in a collaborator BeanA;
@Component
public class BeanA {
@Autowired
private BeanB dependency;
//...
}
Now if the dependency BeanB is not defined in the Spring Context, then the bootstrap will fail with the no such bean definition.
Is defined:
Another reason for this exception is the existence of the two beans in the context, instead of one.
Example:
If an interface, IBeanB is implemented by two beans BeanB1 and BeanB2.
@Component
public class BeanB1 implements IBeanB {
//
}
@Component
public class BeanB2 implements IBeanB {
//
}
If the Bean A auto wires this interface, Spring will not know which one of the two implementations to inject;
@Component
public class BeanA {
@Autowired
private IBeanB dependency;
...
}
Again will result of same error;
Solution:
The solution of this problem is to use the Qualifier annotation to specify exactly the name of the bean you want;
@Component
public class BeanA {
@Autowired
@Qualifier("beanB2")
private IBeanB dependency;
...
}