1. Overview
In this quick tutorial, weβll explain Springβs UnsatisfiedDependencyException, what causes it and how to avoid it.
2. Cause of UnsatisfiedDependencyException
UnsatisfiedDependencyException gets thrown when, as the name suggests, some bean or property dependency isnβt satisfied.
This may happen when a Spring application tries to wire a bean and canβt resolve one of the mandatory dependencies.
3. Example Application
Suppose we have a service class PurchaseDeptService, which depends on InventoryRepository:
@Service
public class PurchaseDeptService {
public PurchaseDeptService(InventoryRepository repository) {
this.repository = repository;
}
}
public interface InventoryRepository {
}
@Repository
public class ShoeRepository implements InventoryRepository {
}
@SpringBootApplication
public class SpringDependenciesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDependenciesExampleApplication.class, args);
}
}
For now, weβll assume that all these classes are located in the same package named com.baeldung.dependency.exception.app.
When we run this Spring Boot application, everything works fine.
Letβs see what kind of issues we can run into if we skip a configuration step.
4. Component Annotation Missing
Now letβs remove the @Repository annotation from our ShoeRepository class:
public class ShoeRepository implements InventoryRepository {
}
When we start our application again, weβll see the following error message: UnsatisfiedDependencyException: Error creating bean with name βpurchaseDeptServiceβ: Unsatisfied dependency expressed through constructor parameter 0
Spring wasnβt instructed to wire a ShoeRepository bean and add it to the application context, so it couldnβt inject it and threw the exception.
Adding the @Repository annotation back onto the ShoeRepository solves the issue.
5. Package Not Scanned
Letβs now put our ShoeRepository (along with InventoryRepository) into a separate package named com.baeldung.dependency.exception.repository.
Once again, when we run our app, it throws the UnsatisfiedDependencyException.
To solve this, we can configure the package scan on the parent package and make sure that all relevant classes are included:
@SpringBootApplication
@ComponentScan(basePackages = {"com.baeldung.dependency.exception"})
public class SpringDependenciesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDependenciesExampleApplication.class, args);
}
}
6. Non-unique Dependency Resolution
Suppose we add another InventoryRepository implementation β DressRepository:
@Repository
public class DressRepository implements InventoryRepository {
}
Now when we run our app, it will once again throw the UnsatisfiedDependencyException.
However, this time the situation is different. As it happens, the dependency cannot be resolved when thereβs more than one bean that satisfies it.
To solve this, we may want to add @Qualifier to distinguish between the repositories:
@Qualifier("dresses")
@Repository
public class DressRepository implements InventoryRepository {
}
@Qualifier("shoes")
@Repository
public class ShoeRepository implements InventoryRepository {
}
Also, weβll have to add a qualifier to PurchaseDeptService constructor dependency:
public PurchaseDeptService(@Qualifier("dresses") InventoryRepository repository) {
this.repository = repository;
}
This will make DressRepository the only viable option, and Spring will inject it into PurchaseDeptService.
7. Conclusion
In this article, we saw several most common cases of encountering UnsatisfiedDependencyException, and then we learned how to solve these problems.
We also have a more general tutorial on Spring BeanCreationException.
