首页 文章

为什么Spring Context被加载两次?

提问于
浏览
5

我有Spring和Spring安全的Web项目 . 我的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>

在服务器日志中,我看到Spring上下文被加载了两次(spring bean初始化,数据库创建......) . DispatcherServlet首次执行此操作,并在secont时间执行ContextLoaderListener . 我该如何解决?

this教程中,我看到如果出现contextParam,则不需要servlet init-params . 但如果我删除init params我有错误:"org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/billboard-servlet.xml]" . Dispather servlet在默认位置查找上下文配置 .

4 回答

  • 2

    您仍然需要servlet的上下文:

    在初始化DispatcherServlet时,Spring MVC在Web应用程序的WEB-INF目录中查找名为[servlet-name] -servlet.xml的文件,并创建在那里定义的bean,覆盖使用该文件定义的任何bean的定义全局范围内的名称 .

    您不需要在 ContextLoaderListener 中将其加载为 context-param .

    只需将 security-config.xml 保留为 context-param (它必须去那里,因为每个应用程序的安全性是全局的),并且 billboard-servlet.xml 作为您的servlet的 contextConfigLocation 它应该可以工作 .

  • 6

    我有同样的问题,原因是:

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

  • 1

    这是两个独立的方法来做同样的事情 . 例如,删除 ContextLoaderListener .

  • 3

    由于你有 spring delegatingFilterProxy ,如果你删除 contextLoaderLister ,你将得到以下异常 .

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

    因此,通过dispatcher servlet通过contextLoaderLister和billboard-servlet.xml加载security-config.xml .

相关问题