2017-05-08 111 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

请不要在问题中添加新的答案,而是编辑问题。 –