SpringBoot - BeanDefinitionOverrideException Invalid bean definition
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
BeanDefinitionOverrideException happens when Spring finds multiple bean definitions with the same name while overriding is disabled. This error is a safety feature that prevents accidental replacement of configuration. The best fix is resolving naming and wiring intent, not blindly enabling global override.
Why the Exception Happens
Common causes:
- Two
@Beanmethods with same method name. - Two components explicitly named the same.
- Test configuration colliding with application bean names.
- Custom beans colliding with auto-configuration names.
Example collision:
Both definitions map to bean name myService.
Fix with Explicit Names and Qualifiers
Give implementations distinct names and inject by qualifier.
Injection:
This makes dependency intent explicit and reviewable.
@Primary Is Not a Name-Collision Fix
@Primary helps when multiple beans of same type exist for autowiring by type. It does not resolve duplicate bean name registration.
Use @Primary only after bean names are distinct and valid.
Control Scan Boundaries
Overly broad scanning can pull in unintended classes and create collisions.
In tests, isolate @TestConfiguration and avoid loading full app context unless required.
Should You Enable Bean Override
Spring allows override via property:
Use this only when replacement is deliberate and documented. As a default policy, it can hide configuration bugs and create surprising environment-specific behavior.
Prefer Conditional Beans for Environment Differences
When different implementations are needed per profile or property, use conditional registration.
Conditional wiring communicates intent better than silent overriding.
Debugging Workflow
When exception occurs:
- Read the two conflicting bean definitions in startup message.
- Search codebase for the duplicated bean name.
- Check imported test configs and auto-config classes.
- Rename or scope beans explicitly.
Enable startup debug report for more context:
This often reveals auto-configuration sources involved in collisions.
Preventive Practices
- Use descriptive bean method names.
- Avoid generic names reused across modules.
- Keep architecture tests for duplicate naming patterns.
- Review new config classes for overlap with existing modules.
Preventing collisions in reviews is cheaper than startup-failure triage later.
Another useful safeguard is module-level naming conventions. For example, prefix infrastructure beans with infra and feature beans with bounded-context names. This lowers collision probability as codebase size grows and makes startup diagnostics easier to interpret when conflicts still occur.
If your team uses shared starter modules, publish a documented list of exported bean names. This avoids accidental collisions when application modules define similarly named custom beans.
This registry also improves architectural visibility during large refactoring efforts.
Common Pitfalls
- Enabling global override as first reaction.
- Assuming
@Primaryresolves same-name registrations. - Reusing generic bean names across unrelated configs.
- Mixing test and production config in one scanned package.
- Ignoring auto-config bean names when defining custom replacements.
Summary
- The exception signals duplicate bean names with override protection active.
- Resolve conflicts with explicit names and qualifier-based injection.
- Use
@Primaryonly for type preference, not name collision fixes. - Prefer conditional registration over global override settings.
- Keep scan scope and config boundaries intentional to avoid future collisions.

