Why Spring and Spring MVC Need Parent‑Child Containers
The article explains the concept of parent‑child containers in Spring, outlines three key benefits—modularization, scoped bean visibility, and configuration reuse—provides step‑by‑step XML and Java code examples, and shows how the containers integrate with Spring MVC to create a cleaner, more maintainable application architecture.
What Is a Parent‑Child Container?
In Spring, an ApplicationContext can be arranged hierarchically so that one context becomes the parent of another. The child inherits bean definitions from the parent while being able to declare its own beans, enabling modular organization and reuse.
Why Use Parent‑Child Containers?
1. Modular Development
Large enterprise applications often consist of modules such as user management, order processing, and payment integration. By placing each module in its own child container and sharing common beans from the parent, code and configuration duplication are reduced, improving modularity.
2. Scope Limitation
Beans defined in a child container are visible only within that container, preventing name clashes and allowing fine‑grained control over bean visibility.
3. Configuration Reuse
Common beans—e.g., a data source—can be defined once in the parent and referenced or overridden in children, offering flexible configuration across modules.
Example Code
1. Parent Container Configuration
<!-- parent-context.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
</beans>2. Child Container Configuration
<!-- child-context.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans parent="parent-context.xml"/>
<bean id="userService" class="com.example.service.UserService">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>3. Spring MVC Configuration
<!-- mvc-context.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans parent="child-context.xml"/>
<bean id="mvcHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/user" value-ref="userController"/>
</map>
</property>
</bean>
<bean id="userController" class="com.example.controller.UserController">
<property name="userService" ref="userService"/>
</bean>
</beans>4. Application Startup and Request Flow
During startup, the mvc-context.xml is loaded, creating the full context hierarchy. A user request passes through UserController, which depends on UserService; the service, in turn, uses the shared dataSource bean defined in the parent container.
public class MyAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setParent(getServletApplicationContext());
context.scan("com.example"); // scan for beans
return context;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MvcConfig.class); // register Spring MVC config
return context;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}Conclusion
Using a parent‑child container architecture in Spring and Spring MVC improves modular development, limits bean scope, and enables reuse of common configurations. The provided XML and Java examples demonstrate how the hierarchy simplifies application construction, enhances readability, and supports maintainability as projects grow in complexity.
java1234
Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com
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.
