Spring上下文加载两次

时间:2015-04-08 10:57:08

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

当服务器启动时,应用程序上下文加载两次,如您在日志中看到的那样。当我使用Spring Scheduled annottation时,由于我的应用程序上下文加载两次,它会运行两次。当我删除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>

employee-servlet as

<?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 as

<?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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您只需从web.xml中删除context-param定义:

<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的名称,再次为employee-servlet.xml加载DispatcherServlet的另一个上下文。

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