java dependency_injection_error ai_generated true

Error creating bean with name 'X': Unsatisfied dependency expressed through constructor parameter

ID: java/spring-bean-creation-error

Also available as: JSON · Markdown
90%Fix Rate
91%Confidence
58Evidence
2022-11-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
17 active

Root Cause

Spring cannot create a bean because a required dependency is missing from the application context. This is typically caused by a missing @Component/@Service annotation, incorrect component scan paths, or circular dependencies. The error message identifies the bean name and the unsatisfied dependency type.

generic

Workarounds

  1. 92% success Verify component scan paths include the package containing the missing bean
    Check your @SpringBootApplication or @ComponentScan annotation's basePackages. The default scan starts from the package of the main application class. If the missing bean is in a different package tree (common in multi-module projects), add its package explicitly: @ComponentScan(basePackages = {"com.example.app", "com.example.shared"}). Run with --debug flag to see which packages are scanned.

    Sources: https://docs.spring.io/spring-framework/reference/core/beans/classpath-scanning.html

  2. 90% success Ensure the missing class has @Service, @Component, @Repository, or @Configuration annotation
    Check the class that Spring cannot find. It must have a stereotype annotation (@Service, @Component, @Repository, @Controller) or be declared as a @Bean in a @Configuration class. The error message 'No qualifying bean of type X' tells you the exact type to look for. Search your codebase for the class and add the appropriate annotation.

    Sources: https://docs.spring.io/spring-framework/reference/core/beans/classpath-scanning.html#beans-stereotype-annotations

  3. 85% success Break circular dependencies by applying @Lazy to ONE side of the cycle
    If the error mentions a circular reference (BeanCurrentlyInCreationException), identify the dependency cycle from the stack trace. Add @Lazy to the constructor parameter on ONE side of the cycle: @Lazy @Autowired private final ServiceB serviceB. Better yet, refactor to eliminate the cycle by extracting shared logic into a third bean. Spring Boot 3.x no longer allows circular references by default.

    Sources: https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-lazy-init.html

Dead Ends

Common approaches that don't work:

  1. Adding @Autowired annotation to every field as a shotgun fix 82% fail

    If the dependency bean does not exist in the application context, @Autowired does not create it — it just changes the injection point from constructor to field. The root cause is that the target bean class is not registered as a Spring component. Field injection also makes dependencies implicit, harder to test, and allows creation of objects in invalid states.

  2. Creating empty placeholder beans with @Bean methods to satisfy injection 78% fail

    Placeholder beans provide no actual functionality. The injected dependency appears non-null but its methods either do nothing, return null, or throw UnsupportedOperationException. This silences the startup error but causes NullPointerExceptions or incorrect behavior at runtime, which are much harder to trace back to the missing real implementation.

  3. Adding @Lazy to all injected dependencies to defer the error 70% fail

    Using @Lazy universally defers bean creation from startup to first use. This turns a clear, immediate startup error into a runtime exception that occurs unpredictably when the lazy bean is first accessed — potentially in production under load. It also masks circular dependency errors that Spring would otherwise detect and report at startup.

Error Chain

Leads to:
Preceded by: