Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding the Parent-Child Relationship Between Spring Core and Spring MVC IoC Containers

Tech May 17 1

In the Spring framework, there are two primary IoC (Inversion of Control) containers that operate under a parent-child hierarchy: the Spring Core IoC container and the Spring MVC IoC container. These containers manage different layers of an application and have distinct responsibilities.

1. The Spring Core IoC Container

Definition

  • This is the foundational container for the entire application, responsible for managing all beans at the business logic and data access layers.
  • Its built on the ApplicationContext interface. Common implementations include:
  • AnnotationConfigApplicationContext (annotation-based configuration)
  • ClassPathXmlApplicationContext (XML-based configuration)

Responsibilities

  • Manages beans for service classes, repositories, DAOs, and other non-web components.
  • Provides core features such as dependency injection, aspect-oriented programming (AOP), and transaction management.

Configuration Approaches

  • XML: Using <bean> tags in a separate configuration file.
  • Annotations: Leveraging @Component, @Service, @Repository, and @Autowired.
  • Java Config: Defining beans with @Configuration and @Bean methods.

2. The Spring MVC IoC Container

Definition

  • This container is specialized for the web layer, handling beans related to request handling and response generation.
  • It implements WebApplicationContext, with typical implementations being XmlWebApplicationContext or AnnotationConfigWebApplicationContext.

Responsibilities

  • Manages web-specific beans such as controllers (@Controller), view resolvers, interceptors, and exception handlers.
  • Processes incoming HTTP requests and dispatches them to appropriate controller methods.

Configuration Approaches

  • XML: Using <mvc:annotation-driven> combined with <context:component-scan>.
  • Annotations: Marking classes with @Controller, @RestController, and @RequestMapping.
  • Java Config: Applying @EnableWebMvc alongside @Configuration.

3. Relationship Between Containers

Parent-Child Structure

  • The Spring MVC container is a child of the Spring Core container.
  • The child container has access to all beans defined in the parent, but the parent cannot access child beans.

Separation of Concerns

  • Parent (Core): Manages service and persistence layer beans.
  • Child (MVC): Manages presentation layer beans.

Example Configuration

Core Container (Java Config):


@Configuration
@ComponentScan(basePackages = "com.example.service")
public class AppConfig {
}

MVC Container (Java Config):


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.controller")
public class WebConfig {
}

4. Loading Configurations

XML-Based Setup in web.xml


<!-- Core IoC container -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- MVC IoC container -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Spring Boot Simplification: The @SpringBootApplication annotation automatically configures both containers, using sensible defaults to avoid manual setup.

5. Common Pitfalls and Fixes

  • Bean Not Found Injection Errors: Often occurs when a bean is defined in the wrong container. For example, a @Service placed in the MVC container's scan path. Solution: Ensure business beans are only scanned by the Core container, and web beans only by the MVC container.
  • Duplicate Bean Scanning: Hapens when both containers scan overlapping packages. Prevention: Define mutually exclusive base package paths for each @ComponentScan.
  • Name Conflicts Across Containers: If a bean with the same ID exists in both containers, the child's bean may shadow the parent's. Solution: Avoid identical bean names in both containers unless intentional.

6. Key Takeaways

  • Spring applications operate with two IoC containers: a parent Core container and a child MVC container.
  • The Core container manages non-web beans (services, repositories), while the MVC container handles web beans (controllers, interceptors).
  • Adhering to this separation prevents runtime errors and improves code organization. Controllers should not be scanned by the Core container, and business services should not be scanned by the MVC container.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.