I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

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 Spring application tries to wire a bean and cannot 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, hence 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’ll 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’ve seen several most common cases of encountering UnsatisfiedDependencyException. Also, we’ve learned how to solve these problems.

You may also want to look at the more general tutorial on Spring BeanCreationException.

The code presented here can be found over on GitHub.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS