由于加倍的上下文(servlet + ContextLoaderListener),所有Spring Framework bean都会重复

Kin*_*cka 4 java spring servlets spring-mvc

  • 如果我通过调度程序servlet创建spring上下文,我在DelegatingFilterProxyfilter中遇到错误:

    java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:251)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66)
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果我创建Spring上下文ContextLoaderListener我只是有404错误怎么一回事,因为中没有的servlet

  • 如果我通过servlet和listener创建spring上下文,我有重复的上下文,所以所有bean都是重复的,包括带有请求映射的控制器,双执行@Scheduled方法等.

如何在不重复上下文的情况下创建高级spring应用程序(包括大量过滤器等)?

我的web.xml:

<?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"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">

        <display-name>MyWebApplication</display-name>

        <servlet>
                <servlet-name>springDispatcher</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <init-param>
                        <param-name>contextConfigLocation</param-name>
                        <param-value>/WEB-INF/spring.xml</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
                <servlet-name>springDispatcher</servlet-name>
                <url-pattern>/</url-pattern>
        </servlet-mapping>

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

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

        <!-- UTF-8 -->
        <filter>
             <filter-name>encoding-filter</filter-name>
             <filter-class>
                 org.springframework.web.filter.CharacterEncodingFilter
             </filter-class>
             <init-param>
                 <param-name>encoding</param-name>
                 <param-value>UTF-8</param-value>
             </init-param>
             <init-param>
             <param-name>forceEncoding</param-name>
             <param-value>true</param-value>
             </init-param>
         </filter>

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

         <!-- Spring Security -->
        <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>
Run Code Online (Sandbox Code Playgroud)

Ral*_*lph 7

如果你使用弹簧相关的servlet过滤器并使用mvc控制器,那么你需要:

  • ContextLoaderListener AND
  • DispatcherServlet的弹簧构

(请参阅ContextLoaderListener与否?)

两者都创建自己的servlet上下文.ContextLoaderListener创建父上下文(有时称为内部上下文).DispatcherServlet创建一个子上下文(父上下文)(有时称为外部上下文).子上下文的bean可以访问父上下文的bean,但不能相反.

在一个不太简单的Web应用程序中,您需要两个上下文,因为有许多servlet过滤器需要已经创建的spring上下文.另一方面,所有控制器东西都需要一个ServletContext,而这只是由Dispatcher Servlet创建的.

另一点是,你不应该每次创建两个bean(有时候这没问题,有时候也没问题).所以你需要有两个弹簧配置,一个用于内部上下文,一个用于另一个上下文.而且你需要为每个bean决定它是属于内部还是外部.

经验法则是:将所有内容放在内部上下文中,除了那些需要Servlet上下文或者与Web前端密切相关的东西,比如MVC-Controllers,Tiles配置,....