2017-05-08 60 views
0

我試圖集成Quartz Scheduler和Spring 4.但是,我注意到我所有的類都加載了兩次。我搜索了一下,發現它是由於Dispatcher Servlet Loader和ContetxtListener Loader加載XML配置而發生的。並刪除條目將解決這個問題。但是在我的web.xml中,我沒有這樣的條目。 還有什麼我可能做錯的幫助?春季4上下文加載兩次

編輯: 我已經將我的項目減少到使用Quratz Scheduler的SPRING的非常基本的實現,並且它總是加載兩次。通過Quartz Scheduler調用的應用程序中只有一個類(POJO)。 POJO使用對象的哈希碼和當前時間在控制檯上打印消息。該消息用兩個不同的哈希碼同時打印兩次。 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> 

和更新的調度,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文件中的錯誤?

+0

它們是真的被加載兩次還是隻是有一個錯誤的日誌記錄設置...並且你沒有在你的代碼中的任何地方做一個''''''''''''''''''''''''''''''''' –

+0

奇怪,是否有可能從Spring的ContextLoaderListener延伸出來的com.soft.utils.SessionListener?如果是這樣,它將首先加載applicationContext.xml,然後tractivity-servlet.xml – ootero

+0

它們確實加載了兩次。 SessionListener不從ContextLoaderListener擴展。 我編輯了問題,並刪除了SessionListener和其他東西,但它仍然加載上下文兩次。 –

回答

0

這個問題爲我解決了。它歸功於Eclipse的Eclipse和Tomcat插件。我在Eclipse中的項目名稱與ContextRoot不同。 Eclipse將應用程序部署到Tomcat一次,但該應用程序可通過2個URL訪問,即一個用於ProjectName,另一個用於上下文根。

1)http://localhost:8080/TractivityPhase2/login - >有關的項目名稱

2)http://localhost:8080/Tractivity/login - >對於上下文根

所以實際的應用程序是爲2個網址加載兩次。

當我將Context Root等同於我的項目名稱時,我解決了這個問題。希望這個答案可以幫助別人。

+0

請不要在問題中添加新的答案,而是編輯問題。 –