0

I am trying to setup a Web application that uses

  1. Spring 3.2.x
  2. Spring Security 3.x
  3. Atmosphere 2.x

I started using this example SpringMVC-Atmosphere-Example.

Using this web.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" id="WebApp_ID" metadata-complete="true" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>Example Project</display-name>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>gui-dispatcher</servlet-name>
    <servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class>
    <async-supported>true</async-supported>

    <init-param>
        <param-name>org.atmosphere.servlet</param-name>
        <param-value>org.springframework.web.servlet.DispatcherServlet</param-value>
    </init-param>

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-context.xml</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.cpr.broadcasterClass</param-name>
        <param-value>org.atmosphere.cpr.DefaultBroadcaster</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.cpr.broadcaster.shareableThreadPool</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.useNative</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.useWebSocket</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>org.atmosphere.useStream</param-name>
        <param-value>true</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>gui-dispatcher</servlet-name>
    <url-pattern>/main/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/security/spring-security.xml
        /WEB-INF/spring-context.xml
    </param-value>
</context-param>


<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
</web-app>

I get the Spring application-context for the Spring Dispatcher servlet to be loaded twice. Why is it loaded twice and how can I prevent this duplicate context loading?

2

2 Answers 2

6

It is because you have both a org.springframework.web.context.ContextLoaderListener and a org.springframework.web.servlet.DispatcherServlet defined within your web.xml, processing the same spring-context.xml

org.springframework.web.context.ContextLoaderListener will be invoked when you first deploy your web application and will create a single Spring ApplicationContext using /WEB-INF/spring-context.xml and /WEB-INF/security/spring-security.xml.

Next, the Servlets for your webapp will be created, one of which appears to delegate to a Spring Dispatcher servlet. This will create a new ApplicationContext using only /WEB-INF/spring-context.xml.

This is why you see your ApplicationContext being created twice. To prevent it, use either the DispatcherServlet or the ContextLoaderListener to create your ApplicationContext. Given your need to integrate with another framework, I suspect the easiest option might be to use the DispatcherServlet.

1

Use use-default-filters on applicationContext.xml and mvc-dispatcher-servlet.xml.

  1. applicationContext.xml
    <context:component-scan base-package="com.companyname">
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
  2. mvc-dispatcher-servlet.xml
    <context:component-scan base-package="com.companyname" use-default-filters="false">
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
  3. Remove applicationContext.xml and mvc-dispatcher-servlet.xml reference from web.xml

  4. At applicationContext, import Security-context.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.