Understanding Bean Definition Overriding in Spring Boot 2.0.3: @Configuration vs @Component
This article explains why Spring Boot creates only one UserManager bean when both @Configuration + @Bean and @Component definitions exist, how the bean definition overriding mechanism works across different Spring versions, and how to control it with the allowBeanDefinitionOverriding setting.
The author encountered a situation where a class annotated with both @Configuration + @Bean and @Component produced two possible UserManager beans, but only one was injected at runtime. By examining the Spring container, the article shows that the @Configuration + @Bean definition overrides the @Component definition.
Using Spring Boot 2.0.3 (Spring Framework 5.0.7.RELEASE), the default bean scope is singleton. The author verifies the number of UserManager instances by debugging the constructors; only the constructor used by the @Configuration bean is called.
Source code inspection reveals that during component scanning, the UserManager class is first registered as a ScannedGenericBeanDefinition (from @Component). Later, when processing @Configuration classes, a ConfigurationClassBeanDefinition with the same bean name replaces the previous definition.
Spring logs an INFO‑level message about the overriding, for example:
2021-10-03 20:37:33.697 INFO 13600 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'userManager' with a different definition: replacing [Generic bean: class [com.lee.qsl.manager.UserManager]; scope=singleton; ...] with [Root bean: class [null]; factoryBeanName=userConfig; factoryMethodName=userManager; ...]In Spring 5.1.2 (Spring Boot 2.1.0) the behavior was changed: bean definition overriding can be disabled, causing the application to fail fast with an error like:
The bean 'userManager', defined in class path resource [com/lee/qsl/config/UserConfig.class], could not be registered. A bean with that name has already been defined in file [D:\qsl-project\spring-boot-bean-component\target\classes\com\lee\qsl\manager\UserManager.class] and overriding is disabled.The new property spring.main.allow-bean-definition-overriding (or the underlying allowBeanDefinitionOverriding in DefaultListableBeanFactory) lets developers decide whether overriding is permitted. By default, Spring itself allows overriding, but Spring Boot 2.1.0 disables it unless the property is set to true.
Finally, the article summarizes that Spring 5.0.7 allows both annotations to coexist with the @Configuration bean winning, while later versions provide explicit configuration to control this behavior.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
