java - 为什么 Spring Context 会加载两次?

标签 java spring

我有一个带有 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 做,第二次是 ContextLoaderListener。我该如何解决?

this教程我看到如果出现 contextParam,则不需要 servlet init-params。但是如果我删除 init params 我有错误:“org.apache.catalina.LifecycleException:org.apache.catalina.LifecycleException:java.io.FileNotFoundException:无法打开 ServletContext 资源 [/WEB-INF/billboard-servlet.xml] ”。 Dispather servlet 在默认位置查找上下文配置。

最佳答案

您仍然需要 servlet 的上下文:

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

虽然您不需要将它作为 context-param 加载到 ContextLoaderListener 中。

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

关于java - 为什么 Spring Context 会加载两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11409237/

相关文章:

spring - Model、ModelMap、ModelAndView有什么区别?

java - 如何在 Spring Data Rest 中格式化@param字符串

JavaFX:如何知道应用程序正在哪个系统上运行?

java - 在没有 IllegalMonitorStateException 的情况下解锁 ReentrantLock

java - 为网站添加额外的安全性

java - 摆脱 "No Hibernate Session bound to thread"并在读取方法上使用 @Transactional 进行性能

java - 如何从 JAX Web 服务中访问原始请求 xml?

java - spring:通过读取另一个 bean 的属性来设置一个 bean 的属性?

java - 如何使用准备好的语句的占位符来绑定(bind)表名的变量

java - 如何过滤掉 SWT DirectoryDialog 中的 "My Computer"-选项?