从 Atmosphere 的 MeteorServlet 中使用 Spring DispatcherServlet 时,Spring 上下文会加载两次

Pet*_*ris 0 java spring spring-mvc spring-security

我正在尝试设置一个使用的 Web 应用程序

  1. 弹簧 3.2.x
  2. 春季安全 3.x
  3. 气氛 2.x

我开始使用这个例子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>
Run Code Online (Sandbox Code Playgroud)

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

Rob*_*ake 6

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

org.springframework.web.context.ContextLoaderListener将在您首次部署 Web 应用程序时调用,并将ApplicationContext使用 /WEB-INF/spring-context.xml/WEB-INF/security/spring-security.xml.

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

这就是为什么你看到你ApplicationContext被创造了两次。要防止它,请使用DispatcherServletContextLoaderListener来创建您的ApplicationContext. 鉴于您需要与另一个框架集成,我怀疑最简单的选择可能是使用DispatcherServlet.