Backend Development 8 min read

Understanding @Configuration vs @Component Bean Overriding in Spring Boot 2.0.3

This article explains why a Spring Boot 2.0.3 application creates two UserManager beans when both @Configuration + @Bean and @Component are used, how the container decides which instance is injected, and how newer Spring versions allow developers to control bean‑definition overriding via configuration.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding @Configuration vs @Component Bean Overriding in Spring Boot 2.0.3

Problem Description

During development a class was annotated both with @Configuration and @Component . The @Configuration method creates a UserManager bean whose userName field is non‑null, while the @Component creates another bean with userName set to null . The question is which bean is actually injected into other components.

The project has been running for a long time without errors, and the original intention was to make the @Configuration bean effective, which indeed happened.

Key details:

Spring Boot version: 2.0.3.RELEASE

Bean scope: default singleton

Result Verification

By debugging and checking the source code, we can see how many UserManager instances exist. Inspecting the constructors shows that only the parameterized constructor is invoked, indicating a single bean creation.

The conclusion is that the injected bean is the one created by @Configuration + @Bean , because that is the only bean whose constructor was called.

Why isn’t the @Component bean (with userName null) injected?

Source Code Analysis

The relationship between @Configuration and @Component is tight: a class annotated with @Configuration is also eligible for component scanning.

The ConfigurationClassPostProcessor processes @Configuration classes and implements both BeanFactoryPostProcessor and PriorityOrdered . During the AbstractApplicationContext.refresh() call, invokeBeanFactoryPostProcessors(beanFactory) triggers scanning of the com.lee.qsl package, detecting UserConfig , UserController , and UserManager .

At this point the @Component version of UserManager is registered as a ScannedGenericBeanDefinition . Later, the @Configuration method defined in UserConfig creates a ConfigurationClassBeanDefinition for the same bean name, which overrides the previous definition.

When the bean is finally instantiated, Spring uses the definition from the @Configuration class, invoking the parameterized constructor and producing the non‑null userName instance.

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.RELEASE (Spring Boot 2.1.0.RELEASE), the framework introduced the allowBeanDefinitionOverriding property, letting developers decide whether bean definitions may be overridden.

When this property is set to true in application.properties , the overriding behavior is permitted; otherwise, an exception is thrown at startup if a duplicate bean name is detected.

Spring Boot 2.1.0 disables overriding by default, overriding Spring's own default (which allows it) and making the behavior explicit to developers.

Conclusion

In Spring 5.0.7.RELEASE (used by Spring Boot 2.0.3), both @Configuration + @Bean and @Component can define the same bean name; the former overrides the latter, and an INFO‑level log is emitted.

From Spring 5.1.2.RELEASE onward, the framework provides the allowBeanDefinitionOverriding configuration option, giving developers control over whether such overrides are allowed.

Thus, the injected UserManager bean in the original scenario is the one created by the @Configuration method.

Backend DevelopmentConfigurationcomponentSpring BootBean Overriding
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.