Why Spring and Spring MVC Use Parent‑Child Containers

The article explains how Spring’s parent‑child container pattern separates core beans like DataSource and TransactionManager from business‑level beans, improving modularity, configuration reuse, decoupling, and flexible bean management, and shows concrete XML and Spring Boot examples for defining and loading these containers.

java1234
java1234
java1234
Why Spring and Spring MVC Use Parent‑Child Containers

Concept of Parent‑Child Containers

In Spring the IoC container manages bean lifecycles. A parent container holds core beans such as DataSource and TransactionManager that are shared globally. A child container inherits the parent’s bean definitions and can define its own beans, for example controllers and services.

Benefits of the Parent‑Child Model

Modular management : core bean configuration is separated from business‑specific beans, making the architecture clearer and easier to extend.

Configuration reuse : beans defined in the parent can be reused by multiple child containers, avoiding duplication.

Decoupling : modules depend less on each other; Spring MVC controllers can reside in a child container while the parent provides shared resources.

Flexible bean management : child containers can override or extend parent beans to meet specific business needs.

Parent‑Child Relationship in Spring MVC

The DispatcherServlet acts as a front‑controller and creates its own child container. The child container handles controller beans and view resolution, while the parent container manages application‑wide beans such as the DataSource and TransactionManager.

Defining Parent and Child Containers

Parent configuration (applicationContext‑parent.xml)

<!-- applicationContext-parent.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-3.0.xsd">

    <!-- DataSource bean -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
    </bean>

    <!-- TransactionManager bean -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

Child configuration (applicationContext‑child.xml)

<!-- applicationContext-child.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-3.0.xsd"
       parent="applicationContext-parent">

    <!-- Controller bean -->
    <bean id="userController" class="com.example.UserController">
        <property name="userService" ref="userService"/>
    </bean>

    <!-- Service bean -->
    <bean id="userService" class="com.example.UserService">
        <property name="userRepository" ref="userRepository"/>
    </bean>

    <!-- Repository bean -->
    <bean id="userRepository" class="com.example.UserRepository"/>
</beans>

Because the child container declares parent="applicationContext-parent", it inherits the DataSource and TransactionManager beans defined in the parent, so those beans need not be re‑declared.

Using the Model in Spring Boot

XML configurations are loaded with @ImportResource (or a @Configuration class). Example:

@SpringBootApplication
@ImportResource("classpath:applicationContext-child.xml")
public class SpringBootApp {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApp.class, args);
    }
}

Conclusion

The parent‑child container pattern enables modular management, configuration reuse, reduced coupling, and flexible bean handling, allowing developers to organize large, complex Spring and Spring MVC applications more cleanly and maintainably.

IoCSpringSpring BootSpring MVCBean ConfigurationParent-Child Container
java1234
Written by

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

0 followers
Reader feedback

How this landed with the community

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.