Spring 4上下文加载两次

时间:2017-05-08 17:18:28

标签: java spring spring-mvc

我试图将Quartz Scheduler与Spring 4集成。但是,我注意到我的所有类都加载了两次。我搜索了一下,发现它是由于Dispatcher Servlet Loader和ContetxtListener Loader加载XML配置而发生的。删除条目将解决问题。但是在我的web.xml中,我没有这样的条目。 关于我可能做错什么的任何帮助?

EDITED: 我已经使用Quratz Scheduler将我的项目简化为一个非常基本的SPRING实现,它总是加载两次。应用程序中只有一个类(POJO)通过Quartz Scheduler调用。 POJO在控制台上使用对象的哈希码和当前时间打印消息。消息使用2个不同的哈希码同时打印两次。 2个不同的哈希码暗示了上下文的2个加载。

项目结构如下: enter image description here

下面给出了更新的web.xml(没有会话监听器)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" 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">
<display-name>Spring MVC Application</display-name>
<welcome-file-list>
    <welcome-file>createIdeaPublic</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<display-name>Tractivity</display-name>
<session-config>
    <session-timeout>120</session-timeout>
</session-config>
   <servlet>
     <servlet-name>Tractivity</servlet-name>
     <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
     </servlet-class>
     <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
     <servlet-name>Tractivity</servlet-name>
     <url-pattern>/</url-pattern>
   </servlet-mapping>
  <error-page>
   <location>/jsp/components/jspError.jsp</location>
</error-page>
</web-app>

更新后的Dispatcher-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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-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/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="quartzExample" />
    <property name="targetMethod" value="printMessage" />
</bean>
<bean id="quartzExample" class="com.soft.quartz.QuartzExample">
</bean>
<bean id="cronTrigger"  class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="simpleJobDetail" />
    <property name="cronExpression" value="0/3 * * * * ? *" />
</bean>
<bean  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cronTrigger" />
        </list>
    </property>
</bean>
</beans>

通过Quartz Scheduler调用的POJO的定义是:

package com.soft.quartz;

import java.util.Date;

public class QuartzExample {

    public void printMessage(){
        System.out.println("Hello Quartz "+this.hashCode()+" " + (new Date()));
    }

}

是否知道这个特定版本的Spring jar文件中是否存在错误?

1 个答案:

答案 0 :(得分:0)

这个问题已经解决了。它归功于Eclipse的Eclipse和Tomcat插件。我在Eclipse中的项目名称与ContextRoot不同。 Eclipse将应用程序部署到Tomcat一次,但应用程序可通过2个URL访问,即一个用于ProjectName,一个用于Context Root。

1)http://localhost:8080/TractivityPhase2/login - &gt;对于项目名称

2)http://localhost:8080/Tractivity/login - &gt;对于ContextRoot

实际上,应用程序为2个URL加载了两次。

当我将Context Root等同于我的项目名称时,我的问题得到了解决。希望这个答案有助于其他人。