All Spring Framework beans are duplicated due to the double context (servlet + ContextLoaderListener)

  • If I create a spring context using the dispatcher servlet , I got an error in DelegatingFilterProxy:

    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)
    
  • If I create a spring context , I just have a beacuse error without a servlet ContextLoaderListener404

  • If I create a spring context using a servlet and a listener , I have a duplicate context, so all beans are duplicated, including controllers with query mappings, double-execution @Scheduledmethods, etc.

How to create an extended spring application (including many filters, etc.) without a duplicate context?

My 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>
+4
1

spring, mvc, :

  • ContextLoaderListener AND
  • DispatcherServlet- spring -

(. ContextLoaderListener ?)

. ContextLoaderListener parent-context ( ). DispatcherServlet child-context ( ) ( ). beans beans , .

- , , spring. , ServletContext, Servlet-.

, bean ( , ). spring, , . bean, .

: , , -, MVC-, ,....

+7

Source: https://habr.com/ru/post/1532009/


All Articles