java - Why Spring Context is loaded twice? -


I have a web project with Spring and Spring Security. Xml:

  & lt; Web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http: // www. W3.org/2001/XMLSchema -instance" xsi: schema location = "http : //java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0xsd "version =" 3.0 "& gt; & Lt; Display-name & gt; Billboard & lt; / Display-name & gt; & Lt; Session-config & gt; & Lt; Session timeout & gt; 30 & lt; / Session-timeout & gt; & Lt; / Session-config & gt; & Lt; Listener & gt; & Lt; Audience category & gt; Org.springframework.web.context.ContextLoaderListener & lt; / Listener Category & gt; & Lt; / Listener & gt; & Lt; Listener & gt; & Lt; Audience category & gt; Org.springframework.security.web.session.HttpSessionEventPublisher & lt; / Listener Category & gt; & Lt; / Listener & gt; & Lt; Context param & gt; & Lt; Ultimate Name & gt; ContextConfigLocation & lt; / Lastname & gt; & Lt; Param-pricing & gt; Square Path: Security-config.xml classpath: billboard-servlet.xml & lt; / Param-value & gt; & Lt; / Reference param & gt; & Lt; Servlet & gt; & Lt; Servlet-name & gt; Board & lt; / Servlet-name & gt; & Lt; Servlet category & gt; Org.springframework.web.servlet.DispatcherServlet & lt; / Servlet category & gt; & Lt; Init-param & gt; & Lt; Ultimate Name & gt; ContextConfigLocation & lt; / Lastname & gt; & Lt; Param-pricing & gt; Square Path: Security-config.xml classpath: billboard-servlet.xml & lt; / Param-value & gt; & Lt; / Init-param & gt; & Lt; Load-On-Startup & gt; 1 & lt; / Load-on-startup & gt; & Lt; / Servlet & gt; & Lt; Servlet-mapping & gt; & Lt; Servlet-name & gt; Board & lt; / Servlet-name & gt; & Lt; URL pattern & gt; * Html & Lt; / Url pattern & gt; & Lt; / Servlet-mapping & gt; & Lt; Filter & gt; & Lt; Filter-name & gt; SpringSecurityFilterChain & lt; / Filter-name & gt; & Lt; Filter range & gt; Org.springframework.web.filter.DelegatingFilterProxy & lt; / Filter range & gt; & Lt; / Filter & gt; & Lt; Filter-mapping & gt; & Lt; Filter-name & gt; SpringSecurityFilterChain & lt; / Filter-name & gt; & Lt; URL pattern & gt; / * & Lt; / URL pattern & gt; & Lt; / Filter-mapping & gt; & Lt; / Web application & gt;   

In the server log I see that the spring context loads twice (spring bean initialization, database creativity ...). DispatcherServlet does this for the first time, and secont time at ContextLoaderListener. How can I fix this?

In the tutorial, I see that if the reference para is presented then the servlet init-params are not required. But if I remove the init params then I get an error: "org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.io.FileNotFoundException: Servicelet Contact resource can not be opened [/ WEB-INF / billboard -servlet.xml] ". Dispather servlet gets context-configurations at default location.

You still need a reference for your servlet:

< P> At the start of a dispatcher service, Spring MVC looks for the web-INF directory of your web application [servlet-name] -Serverlet.XM, and overrides defined definitions in the global scope with the same name Define any of the beans.

You do not need to load it in ContextLoaderListener as context-param though.

Just leave security-config.xml as the context-absolute (it's there to go there because security is globally applied) , And billboard-servlet.xml as contextconfigLocation of your servlet and it should work.

Comments