【问题标题】:Spring contex load twiceSpring上下文加载两次
【发布时间】:2015-04-08 10:57:08
【问题描述】:

服务器启动时,应用程序上下文加载了两次,如您在日志中所见。当我使用 Spring Scheduled 注释时,它会运行两次,因为我的应用程序上下文加载了两次。当我删除 contextLoadListener 时,调度程序工作得很好(只有一次),但这次 web mvc 不起作用。

INFO: Initializing Spring root WebApplicationContext
INFO: Initializing Spring FrameworkServlet 'employee'

我的 web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>xxxx</display-name>
    <servlet>
        <servlet-name>employee</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>employee</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/employee-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>

员工-servlet 为

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

    <context:annotation-config />
    <context:component-scan base-package="xxx.controller" />
    <context:component-scan base-package="xxx.service"/>
    <context:component-scan base-package="xxx.dao"/>
    <context:component-scan base-package="xxx.dvo"/>

    <task:annotation-driven />
    <bean id="demoServiceBasicUsageFixedDelay" class="xxx.dao.BaseDao"></bean>

    <import resource="classpath:spring-security-config.xml" />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>

    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />


    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

Spring-security.xml 为

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

    <http auto-config="true" disable-url-rewriting="true" use-expressions="true">
        <form-login login-page="/signin" authentication-failure-url="/signin?error=1"/>
        <logout logout-url="/logout" />
        <intercept-url pattern="/resources/**" access="permitAll" />
        <intercept-url pattern="/signin" access="permitAll" />
        <!-- <intercept-url pattern="/result/**" access="permitAll" /> -->
        <intercept-url pattern="/**" access="isAuthenticated()" /> 
    </http>

    <authentication-manager erase-credentials="true" >
        <authentication-provider user-service-ref="userService">
        </authentication-provider>
    </authentication-manager>


</beans:beans>

【问题讨论】:

    标签: java spring spring-mvc scheduled-tasks applicationcontext


    【解决方案1】:

    它被加载了两次,因为 spring 默认使用 servlet 名称 (employee-servlet.xml) 为 servlet 调度程序读取一个文件。要使 spring 不加载它两次,您必须更改 contextConfigLocation 文件的名称。

    【讨论】:

    • 当我将 employee-servlet.xml 文件名和 contextConfigLocation 的名称更改为 employee-servlet2.xml 我得到 java.io.FileNotFoundException:无法打开 ServletContext 资源 [/WEB-INF/employee -servlet.xml 异常
    • 我删除了 1,现在可以正常使用了。这是真的吗?
    • 删除它不是一个好主意,您应该为调度程序 servlet 添加另一个名为“employee-servlet.xml”的文件,并在其中存储相关的 bean,如 InternalResourceViewResolver 或 mvc 的.
    【解决方案2】:

    您只需从 web.xml 中删除上下文参数定义:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/employee-servlet.xml</param-value>
    </context-param>
    

    Spring 只会加载一次employee-servlet.xml。

    解释

    使用 contextConfigLocation 告诉 Spring 使用该 xml (employee-servlet.xml) 作为您的根应用程序上下文。

    Spring 总是根据 servlet 的名称为 DispatcherServlet 加载另一个上下文,同样是employee-servlet.xml。

    因此,您最终会加载相同的上下文两次。

    【讨论】:

      猜你喜欢
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2013-11-22
      • 2012-10-04
      • 2014-08-15
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多