Techniques Spring Uses to Create Beans from beans.xml — The Hidden XML Parsing Journey Revealed
In the modern Spring Boot world, where annotations like @Configuration and @ComponentScan dominate, it’s easy to forget that the roots of Spring lie in XML-based configuration. But here’s the thing — even when you use annotations, the core mechan...

I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
1. Introduction: Why Understanding XML Bean Creation Still Matters
1.1 How XML Configuration Fits into the Spring IoC Container
<!-- beans.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="greetingService" class="com.example.GreetingService"/>
<bean id="userController" class="com.example.UserController">
<property name="greetingService" ref="greetingService"/>
</bean>
</beans>
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserController controller = context.getBean(UserController.class);
controller.greet();
}
}
1.2 Step-by-Step: How Spring Parses the XML
new ClassPathXmlApplicationContext("beans.xml");
- ClassPathXmlApplicationContext delegates to a DefaultListableBeanFactory.
- XmlBeanDefinitionReader is created to handle the parsing.
- XmlBeanDefinitionReader.loadBeanDefinitions() reads the XML resource.
- BeanDefinitionDocumentReader and DefaultBeanDefinitionDocumentReader traverse the XML DOM structure.
- For every element, a BeanDefinition object is built using a BeanDefinitionParserDelegate.
- Finally, all BeanDefinitions are stored in the BeanFactory registry.
1.3 The Core Classes Involved in XML Parsing
- XmlBeanDefinitionReader: Acts as the entry point, responsible for loading XML resources and triggering document parsing.
- BeanDefinitionDocumentReader: Defines how the root element should be interpreted.
- BeanDefinitionParserDelegate: The heavy lifter that parses attributes like id, class, scope, and nested elements like .
- DefaultListableBeanFactory: The ultimate container that stores every parsed BeanDefinition.
1.4 Code Example: Custom Logging of XML Parsing
public class CustomXmlReader extends XmlBeanDefinitionReader {
public CustomXmlReader(BeanDefinitionRegistry registry) {
super(registry);
}
@Override
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException {
System.out.println(">>> Parsing XML: " + resource.getFilename());
return super.doLoadBeanDefinitions(inputSource, resource);
}
}
public class CustomContext extends ClassPathXmlApplicationContext {
@Override
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
super.initBeanDefinitionReader(new CustomXmlReader(getBeanFactory()));
}
}
>>> Parsing XML: beans.xml
2. The Deeper Magic: From BeanDefinition to Bean Instance
2.1 The Role of BeanFactory in Bean Instantiation
- Looks up the corresponding BeanDefinition by ID.
- Checks its scope (singleton by default).
- Instantiates the class using reflection.
- Injects dependencies defined via or constructor arguments.
- Applies any BeanPostProcessor (like @Autowired processing).
2.2 How XML Influenced Spring Boot’s Annotation Model
- <bean> → @Bean
- <context:component-scan> → @ComponentScan
- <property> → @Autowired or constructor injection
2.3 Why You Should Care as a Developer
- Debug BeanCreationException at a much deeper level.
- Write custom bean factories or namespace handlers (e.g., ).
- Appreciate how Spring Boot auto-configuration builds upon the same core IoC principles.
2.4 Reflection: A Bridge Between XML and Runtime Behavior
Class<?> clazz = Class.forName("com.example.GreetingService");
Object bean = clazz.getDeclaredConstructor().newInstance();
3. Conclusion: XML is Dead? Not Quite.
Read more at : Techniques Spring Uses to Create Beans from beans.xml — The Hidden XML Parsing Journey Revealed





