Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Spring Boot: No qualifying bean of type available

The context could not satisfy a constructor argument. Scan boundaries, missing stereotypes, ambiguous candidates and test slices are the usual reasons.

~4 min readJava
#Spring Boot#Spring#Dependency Injection#Troubleshooting
A Spring context scaffold with an empty cyan bean socket, an amber qualifier tag beside it

Introduction & Quick Summary

text
*************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.example.billing.InvoiceService required a bean of type 'com.example.billing.TaxCalculator' that could not be found. Action: Consider defining a bean of type 'com.example.billing.TaxCalculator' in your configuration.

Spring resolved the dependency graph to the point of InvoiceService, found no candidate for TaxCalculator, and stopped the context refresh. The message names the injection point and the exact type, so the work is finding why that type has no bean, not why the application failed.

The five reasons, in the order Spring Boot users actually hit them:

  • No stereotype annotation. The class is a POJO; without @Component, @Service, @Repository or a @Bean method, nothing registers it.
  • Component scan never reached the package. @SpringBootApplication scans its own package and below. A class in a sibling package, or one moved during a refactor, is invisible.
  • An interface with no implementation registered. The interface is scanned but not instantiable, and the implementation lives in a module that is not on the classpath.
  • More than one candidate, none preferred. Two implementations exist and neither is @Primary nor selected with @Qualifier, which yields the "expected single matching bean but found 2" variant.
  • A condition excluded it. @ConditionalOnProperty, @Profile, or a test slice such as @WebMvcTest deliberately loads only part of the context.
Message variantMeaningFix
required a bean of type ... that could not be foundZero candidatesAnnotate, scan, or define a bean
No qualifying bean of type ... availableZero candidates at lookupSame
expected single matching bean but found 2Ambiguous candidates@Primary or @Qualifier
No bean named 'x' availableNamed lookup, not typeFix @Qualifier("x")
Fails only in testsSlice test contextAdd @MockBean or import the config

Core Mechanics, Signatures & Reproducible Setup

@SpringBootApplication is three annotations: @ComponentScan, @EnableAutoConfiguration and @Configuration. The scan base package is the package of the annotated class, so moving a @SpringBootApplication class into com.example.app makes everything under com.example.services unreachable unless you widen the scan.

A minimal reproduction shows the scan boundary:

text
com/example/app/Application.java @SpringBootApplication com/example/app/InvoiceService.java @Service, injects TaxCalculator com/example/billing/TaxCalculator.java @Service
text
Parameter 0 of constructor in com.example.app.InvoiceService required a bean of type 'com.example.app.TaxCalculator' that could not be found.

Three legitimate fixes, with different costs:

java
// 1. Widen the scan @SpringBootApplication(scanBasePackages = {"com.example.app", "com.example.billing"}) public class Application {} // 2. Move the application class up // com/example/Application.java with packages under com.example.* // 3. Import the module's configuration explicitly @Import(BillingConfig.class) public class Application {}

For a library module that should not expose a scan package, exporting an auto-configuration is cleaner:

java
@AutoConfiguration @ConditionalOnClass(TaxCalculator.class) public class BillingAutoConfiguration { @Bean @ConditionalOnMissingBean TaxCalculator taxCalculator() { return new DefaultTaxCalculator(); } }

Register it in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.

Key Comparison Table

AnnotationRegisters a beanTypical layerMetadata
@ComponentYesAnyNone
@ServiceYesBusiness logicSpecialized @Component
@RepositoryYesPersistenceAdds exception translation
@ControllerYesWebHandler mapping
@Configuration + @BeanYes, one per methodWiringFull proxying by default
@Conditional...Only when matchedAnyControls registration
SituationCheckFix
Class in another moduleIs the jar a dependency?Add it, or import its config
Two implementations--debug condition report@Primary or @Qualifier
Profile-specific beanspring.profiles.activeActivate the profile
Test slice@WebMvcTest, @DataJpaTest@Import, @MockBean
Generic beanType erasure in the messageUse a specific subtype

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: Constructor injection added to a scanned class

java
@Service public class InvoiceService { private final TaxCalculator tax; public InvoiceService(TaxCalculator tax) { this.tax = tax; } // fails at startup }

The error is correct: nothing provides TaxCalculator. Create it as a bean rather than nulling the constructor, because a nullable dependency only moves the NullPointerException to the first request.

Case 2: Two candidates and no qualifier

java
@Service("stripe") class StripePayment implements Payment {} @Service("paypal") class PaypalPayment implements Payment {}
text
No qualifying bean of type 'Payment' available: expected single matching bean but found 2: paypal,stripe
java
@Service public class Checkout { private final Payment payment; public Checkout(@Qualifier("stripe") Payment payment) { this.payment = payment; } }

Naming the parameters to match bean names happens to work in recent Spring versions but is fragile after obfuscation or refactoring; use @Qualifier.

Case 3: Test slices that load only part of the context

java
@WebMvcTest(InvoiceController.class) class InvoiceControllerTest { @MockitoBean TaxCalculator taxCalculator; // Spring Boot 3.4+ }

Before 3.4 the equivalent is @MockBean, which is deprecated later. Without either, the controller's dependency has no bean in the slice and the test fails with the same message as production.

Case 4: Conditional beans that silently disappear

java
@Bean @ConditionalOnProperty(name = "billing.tax.enabled", havingValue = "true") TaxCalculator taxCalculator() { return new DefaultTaxCalculator(); }

Start with --debug or management.endpoint.conditions.enabled=true and read the condition evaluation report, which lists every @Conditional with matched or did-not-match and the reason.

Case 5: Generic beans and erasure

java
interface Handler<T> { void handle(T value); } @Component class StringHandler implements Handler<String> {} @Component class IntegerHandler implements Handler<Integer> {}

Injecting Handler<String> resolves at runtime to Handler, so two candidates appear equal. Declare concrete subtypes or use @Qualifier with named beans.

Case 6: Kotlin classes that are final

A Kotlin class TaxCalculator without open cannot be proxied by Spring AOP, and depending on configuration the failure can surface as a missing candidate when the proxy creation is skipped. Mark the class open or use the all-open plugin for Spring stereotypes.

Common Compilation/Runtime Mistakes & Version Gotchas

  • javax versus jakarta. Spring Boot 3 requires jakarta.*; a class annotated with javax.inject or javax.annotation is never registered. The build succeeds and startup fails, which makes this easy to misread as a scan problem.
  • Duplicate dependencies from two BOMs. Two versions of the same library on the classpath can register neither bean. Run mvn dependency:tree or gradle dependencies and look for conflicting versions.
  • @Configuration inside @Configuration. A nested config class declared static is picked up; a non-static inner class can be skipped depending on scan order.
  • Constructor with too many parameters. Spring does not pick a constructor when there are several and none is annotated; add @Autowired to the intended one, or keep exactly one constructor.
  • Lazy initialization. spring.main.lazy-initialization=true moves the failure from startup to the first call, so a deployment looks healthy and then errors under load.
  • Proxy self-invocation. Calling a @Transactional method from within the same bean bypasses the proxy; the bean exists, but the behavior does not, which is a different bug with the same starting point in the log.

When the context fails during a build, capture the --debug output and filter the condition report; paste the JSON of any actuator conditions response into the JSON formatter to see which conditions failed. If configuration values gate the bean, inspect the active profile files with the env file viewer rather than guessing which profile CI uses. For comparing a working application property file with the failing one, the diff tool makes a one-character key change obvious.

Primary references: Spring Framework dependency injection and collaborators explains autowiring and qualifiers, and structuring your Spring Boot code documents the component scan base package rule.

Official references

primary documentation

The Java documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.