Correctness14 - SpotBugsExtensionForSpringFrameWork/CS5098 GitHub Wiki

Bug pattern name: Component Annotation Missing UnsatisfiedDependencyException (one of BeansExceptions)

Description

UnsatisfiedDependencyException will be thrown when 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.

Example Application below which Component (@Repository) Annotation is Missing
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.

@Service
public class PurchaseDeptService {
    public PurchaseDeptService(InventoryRepository repository) { // Inject
        this.repository = repository;
    }
}
===========================================================================
public interface InventoryRepository {
}
===========================================================================
// Not added @Repository -> UnsatisfiedDependencyException
public class ShoeRepository implements InventoryRepository {
}
===========================================================================
@SpringBootApplication
public class SpringDependenciesExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringDependenciesExampleApplication.class, args);
    }
}

Reasons

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.

Solutions

add @Repository

Reference List

baeldung - spring unsatisfied dependency

⚠️ **GitHub.com Fallback** ⚠️