← Debugging Guides

阅读中文版本

Java — Spring BeanCreationException caused by a dependency

Trace Spring bean creation failures through the caused-by chain to the real root cause and its configuration.

intermediatedebuggingjavaspring

Symptoms

  • Error creating bean with name 'orderService'
  • UnsatisfiedDependencyException during startup
  • The application fails to start after adding a bean
  • A circular dependency message appears

Possible causes

  • A required bean or property is missing from the context
  • A circular dependency between beans
  • A constructor parameter cannot be satisfied by any bean
  • An underlying exception such as a bad property value or a failed connection

Diagnosis steps

Read to the deepest caused-by

App 2>&1 | grep -A3 "Caused by" | tail -20

Interpretation

Spring wraps causes. The last Caused by is the real failure, for example a missing property or a connection error.

Enable debug for bean wiring

--debug --logging.level.org.springframework=DEBUG

Interpretation

The debug output lists conditions and autoconfiguration, showing which bean or property was not satisfied.

Check for a circular dependency

grep -rn "@Autowired\|@Inject" src/main/java

Interpretation

Field injection between two beans often creates a cycle that fails startup.

Root cause

Spring could not construct a bean because its dependencies were not satisfiable, or because constructing it triggered an underlying failure reported through the caused-by chain.

Fix

Satisfy the missing dependency by registering the bean or configuring the property, break cycles with setter or lazy injection or by refactoring, and fix the underlying exception at the deepest cause.

Verify the fix

Start the application and confirm it boots, and confirm the previously missing bean or property is now resolved.

Symptom walkthrough

The application fails to start with BeanCreationException and a chain of caused-by entries. Spring wraps the real mistake, so the top-level message only names the bean; the actionable fact is the last cause, which might be a missing property, an unregistered bean, a circular dependency or a failed external connection attempted during construction.

Fix in practice

Follow the caused-by chain to its deepest link and fix that, rather than the wrapper message. Register missing beans, set the required properties, break dependency cycles by refactoring or using lazy or setter injection, and avoid doing I/O in constructors.

Verification

Start the application and confirm it boots cleanly, then confirm the previously unsatisfied bean or property is now present. A successful startup without the exception confirms the wiring is correct. Keeping bean construction free of external I/O also removes a whole class of startup failures.

Diagnostic commands

Check the behavior with commands like these:

App 2>&1 | grep -A3 "Caused by" | tail -20
--debug --logging.level.org.springframework=DEBUG
grep -rn "@Autowired\|@Inject" src/main/java