从MeteorSerlet of Atmosphere中使用Spring DispatcherServlet时,Spring上下文被加载两次

时间:2014-06-25 11:25:19

标签: java spring spring-mvc spring-security

我正在尝试设置使用

的Web应用程序
  1. Spring 3.2.x
  2. Spring Security 3.x
  3. Atmosphere 2.x
  4. 我开始使用此示例SpringMVC-Atmosphere-Example

    使用此web.xml配置:

    <?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>
    

    我将Spring Dispatcher servlet的Spring应用程序上下文加载两次。为什么它加载了两次,如何防止这种重复的上下文加载?

2 个答案:

答案 0 :(得分:4)

这是因为您在web.xml中定义了org.springframework.web.context.ContextLoaderListenerorg.springframework.web.servlet.DispatcherServlet,处理相同的spring-context.xml

首次部署Web应用程序时将调用

org.springframework.web.context.ContextLoaderListener,并使用/ WEB - ApplicationContextINF/spring-context.xml创建单个Spring /WEB-INF/security/spring-security.xml

接下来,将创建您的webapp的Servlet,其中一个似乎委托给Spring Dispatcher servlet。这将仅使用ApplicationContext创建新的/WEB-INF/spring-context.xml

这就是为什么您看到ApplicationContext被创建两次的原因。要阻止它,请使用DispatcherServletContextLoaderListener创建ApplicationContext。鉴于您需要与其他框架集成,我怀疑最简单的选择可能是使用DispatcherServlet

答案 1 :(得分:1)

在applicationContext.xml和mvc-dispatcher-servlet.xml上使用use-default-filters。

  1. applicationContext.xml


  2. mvc-dispatcher-servlet.xml


  3. 从web.xml中删除applicationContext.xml和mvc-dispatcher-servlet.xml参考

  4. 在applicationContext上,导入Security-context.xml