Spring MVC | Share Root WebApplicationContext Beans with Child WebApplicationContext

spring-240In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. So if you have common beans or resources that need to be shared among “child” WebApplicationContexts,¬† you need to define them within root WebApplicationContext.

About this Post:

Here we will try to access a bean “RootBean” defined in root WebApplicationContext from a Spring MVC Controller residing in a another “child” WebApplicationContext.

spring-root-context( 1 ) Defining Root & Child WebApplicationContext in web.xml

  • web.xml

Root WebApplicationContext¬† is defined using <context-param>…</context-param>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
  <!-- ROOT WebApplicationContext -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
   <!--  WebApplicationContext -->
  <servlet>
  	<servlet-name>rest</servlet-name>  	               
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>rest</servlet-name>
  	<url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

( 2 ) Spring Configuration Files for Root & Child WebApplicationContext

Spring configuration file for “Root” root-context.xml

  • Root context has one bean “rootBean”
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

		<bean class="com.hmkcode.spring.beans.RootBean" name="rootBean" />
</beans>

Spring configuration file for “child” rest-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<context:component-scan base-package="com.hmkcode.spring.controllers" />
	<mvc:annotation-driven />
</beans>

( 3 ) Controller

  • This is a Spring MVC controller that can be called by an HTTP GET request to /rest/controller/get
  • The controller will access “RootBean” which is defined in the root webapplicationcontext.

MyController.java

package com.hmkcode.spring.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmkcode.spring.beans.RootBean;

@Controller
@RequestMapping("/controller")
public class MyController {

	public MyController(){
		System.out.println("new Controller");
	}
	
	@Autowired
	ApplicationContext context;
	
	 @RequestMapping(value = "/get", method = RequestMethod.GET)
	 public @ResponseBody String get() {              
		 
		 RootBean rootBean = (RootBean) context.getBean("rootBean");
		 System.out.println(rootBean);		 
		
		return rootBean.toString();		
	}
			
}

( 4 ) Run & Test

The sample code can be run directly using Maven command

>>mvn jetty:run

Source Code @ GitHub

Leave a Reply