【问题标题】:How to avoid "Root WebApplicationContext: initialization started" loading twice?如何避免“Root WebApplicationContext:初始化开始”加载两次?
【发布时间】:2015-05-31 20:04:39
【问题描述】:

我正在使用 Spring MVC、Spring 安全性并在 Apache Tomcat 1.7x 中进行部署。我注意到 Web 应用程序上下文被加载了两次。请让我知道我的配置有什么问题。

我参考了下面的帖子,但无法识别其中的区别 Why Spring Context is loaded twice?, Spring MVC web app: application context starts twice

INFO ContextLoader:273 - 根 WebApplicationContext: 初始化 开始

下面是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/appServlet/spring-security.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlet and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>
    <session-config>
        <session-timeout>5</session-timeout>
    </session-config>

    <!-- Spring Security Filter -->
    <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>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我的 spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <beans:bean id="roleVoter"
        class="org.springframework.security.access.vote.RoleVoter">
        <beans:property name="rolePrefix" value=""></beans:property>
    </beans:bean>
    <beans:import resource="servlet-context.xml" />

    <beans:bean id="accessDecisionManager"
        class="org.springframework.security.access.vote.AffirmativeBased">
        <beans:constructor-arg name="decisionVoters"
            ref="roleVoter" />
    </beans:bean>

    <http authentication-manager-ref="login-service"
        access-decision-manager-ref="accessDecisionManager">
....
    </http>



    <beans:bean id="userLoginService" class="my.test.service.impl.UserLoginService">
        <beans:property name="userProfileService" ref="userProfileService" />
    </beans:bean>




    <!-- Login message -->
    <beans:bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <beans:property name="basenames">
            <beans:list>
                <beans:value>mymessages</beans:value>
            </beans:list>
        </beans:property>
    </beans:bean>


</beans:beans>

【问题讨论】:

标签: java spring spring-mvc tomcat


【解决方案1】:

有很多方法可以做到这一点。工作方式取决于servlet-context.xml 的情况。

首先,您可以从您的spring-security.xml 中删除&lt;beans:import resource="servlet-context.xml" /&gt;

其他方法是删除这个:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/spring/appServlet/spring-security.xml</param-value>
</context-param>

还有这个:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

并在servlet-context.xml 中包含对spring-security.xml 的导入。

一个好的做法是在多个应用程序上下文中分离您的配置,将所有配置导入applicationContext.xml 并为servlet 上下文设置一个特定的应用程序上下文,类似这样:

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.package" />

    <import resource="applicationContext-security.xml"/>
    <import resource="applicationContext-data.xml"/>

</beans>

applicationContext-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:order="3" p:defaultErrorView="error" />

</beans>

在你的web.xml:

<!-- spring servlet -->
<servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext-servlet.xml</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springDispatcherServlet</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>classpath:META-INF/applicationContext.xml</param-value>
</context-param>

【讨论】:

  • 感谢您的回复。但是删除任何一个都不能解决 bean 依赖关系,因此会引发错误。
  • @ad-inf 如果从 spring-security.xml 中删除导入,则无法解析 bean?您的 servlet-context.xml 看起来如何?改进了答案,看看现在是否对您有帮助。
猜你喜欢
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2023-01-20
  • 2018-02-17
相关资源
最近更新 更多