Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:

>> GET ACCESS NOW

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

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.

The code presented here can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!