Why @Configuration @Bean Overrides @Component in Spring Boot 2.0.3
An in‑depth analysis shows that in Spring Boot 2.0.3 a @Configuration class defining a @Bean can override a @Component‑annotated bean of the same type, how the Spring container registers and replaces the bean definitions, and the evolution of bean‑overriding controls in later Spring and Spring Boot releases.
Problem Description
During development a class annotated with both @Configuration and @Component defines a @Bean method that creates a UserManager instance with a non‑null userName, while a @Component -annotated UserManager would have a null userName. The question is which bean is injected into other components.
Verification
Debugging and constructor logging reveal that only the constructor with arguments is invoked, indicating that a single UserManager instance is created by the @Configuration + @Bean definition.
Source Code Analysis
The @Configuration annotation is itself a @Component, allowing it to be discovered by component scanning. The ConfigurationClassPostProcessor processes @Configuration classes, turning them into ConfigurationClassBeanDefinition objects that replace the original ScannedGenericBeanDefinition for the same bean name.
The bean definition overriding is logged at INFO level:
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]; ...] with [Root bean: class [null]; ... factoryBeanName=userConfig; factoryMethodName=userManager; ...]Spring Upgrade Optimization
Starting with Spring 5.1.2 (Spring Boot 2.1.0) the property allowBeanDefinitionOverriding can be configured. When overriding is disabled, the container throws an error:
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 [...UserManager.class] and overriding is disabled.Spring Boot 2.1.0 introduced its own allowBeanDefinitionOverriding flag (default false) that overrides Spring’s default (which is true), giving developers explicit control.
Summary
In Spring 5.0.7 / Spring Boot 2.0.3 both @Configuration + @Bean and @Component can define the same bean type; the former overrides the latter, and an INFO log records the replacement. Later versions add a configurable property to enable or disable such overriding, making the behavior explicit for developers.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
