Alexandre Bourdin
Alexandre Bourdin

Reputation: 835

Sharing the same Spring context between main ServletContainer and a DispatcherServlet

In my Spring 4 application, I have defined a servlet container for my main application, and a dedicated servlet for a Spring MVC application for the administration dashboard.

My issue is that the Spring context and all beans/services are created twice, thus the services referenced in both contexts are not the same, preventing any runtime manipulation of objects from the admin dashboard (like for example triggering a refresh of some services in the context).

The main web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_3_1.xsd">
  <display-name>REST API</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextClass</param-name>
    <param-value>
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
  </context-param>

  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>60000</param-value>
  </context-param>

  <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>local</param-value>
  </context-param>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      <!-- my config classes -->
    </param-value>
  </context-param>

  <servlet>
    <servlet-name>MyWebService</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value><!-- my.config.class --></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyWebService</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

The web-fragment.xml inlined in my admin application jar:

<?xml version="1.0" encoding="UTF-8"?>
<web-fragment>
  <servlet>
    <servlet-name>admin</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/WEB-INF/admin-servlet.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>admin</servlet-name>
    <url-pattern>/admin/*</url-pattern>
  </servlet-mapping>
</web-fragment>

The admin-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

  <context:component-scan base-package="my.package" />

  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/templates/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <mvc:annotation-driven />
  <mvc:resources mapping="/admin/**" location="/resources/, classpath:/META-INF/resources/" />
  <mvc:default-servlet-handler />

  <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="warnLogCategory" value="apperror" />
    <property name="exceptionMappings">
      <props>
        <prop key="java.lang.Exception">error</prop>
      </props>
    </property>
  </bean>
</beans>

Is there a way to share the same context and beans between the main application and all dispatcher servlets?

Upvotes: 0

Views: 693

Answers (1)

Alexandre Bourdin
Alexandre Bourdin

Reputation: 835

Solution:

because of mixing

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      my.config.class.MyConfig
    </param-value>
  </context-param>

and

<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:/WEB-INF/admin-servlet.xml</param-value>
</init-param>

Spring was creating both an AnnotationConfigWebApplicationContext and an XmlWebApplicationContext which it is apparently not able to merge. By converting my admin-servlet.xml to an equivalent WebMvcConfigurerAdapter implementation, only 1 AnnotationConfigWebApplicationContext is created.

Upvotes: 1

Related Questions