Why does Spring Context load twice?

I have a web project with Spring and Spring protection. My web.xml:

    <web-app 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"
         version="3.0" >
        <display-name>BillBoard
        </display-name>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <listener>
            <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
        </listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:security-config.xml classpath:billboard-servlet.xml</param-value>
        </context-param>
        <servlet>
            <servlet-name>billboard</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:security-config.xml classpath:billboard-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>

        </servlet>
        <servlet-mapping>
            <servlet-name>billboard</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>
        <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>

In the server logs, I see that the Spring context is loaded twice (spring bean initialization, database creation ...). For the first time, DispatcherServlet does this during secont time ContextLoaderListener. How can i fix this?

In this tutorial. I see that if contextParam is presented, then the original servlet parameters are not required. But if I delete init params, I have an error: "org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.io.FileNotFoundException: Could not open the resource ServletContext [/ WEB-INF / billboard-servlet. xml] ". The servlet manager detects the default context configuration.

+5
5

:

DispatcherServlet, Spring MVC [servlet-name] -servlet.xml WEB-INF - beans, beans, .

context-param ContextLoaderListener, .

security-config.xml context-param ( , ) billboard-servlet.xml contextConfigLocation , .

+6

, :

<load-on-startup>1</load-on-startup

+3

, . ContextLoaderListener, .

+2

spring delegatingFilterProxy, contextLoaderLister, .

java.lang.IllegalStateException: No WebApplicationContext found: 
no   ContextLoaderListener registered?

security-config.xml contextLoaderLister billboard-servlet.xml .

+1

Spring MVC Framework XML, :

<!-- for Spring context loader -->
<servlet>
    <servlet-name>billboard</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:security-config.xml classpath:billboard-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>

</servlet>

IoC.

[billboard] , .

Because your dispatcher servlet uses the default context namespace [servlet name] -servlet.xml (in the case of the billboard -servlet.xml), Spring MVC will automatically load it.

For more information see: https://www.conqtech.com/blog/?p=85.

0
source

All Articles