简体   繁体   English

Spring 4 Context加载两次

[英]Spring 4 Context loaded twice

I was trying to integrate Quartz Scheduler with Spring 4. However, I noticed that all my classes are loaded twice. 我试图将Quartz Scheduler与Spring 4集成。但是,我注意到所有类都加载了两次。 I googled a bit and found out that it happens due to the loading of the XML Configuration by both the Dispatcher Servlet Loader and by the ContetxtListener Loader. 我在Google上搜索了一下,发现它的发生是由于Dispatcher Servlet加载程序和ContetxtListener加载程序都加载了XML配置。 And removing the entries will fix the issue. 并删除条目将解决此问题。 However in my web.xml, I have no such entries. 但是,在我的web.xml中,没有此类条目。 Any help on what else I may be doing wrong? 对我可能做错了什么有帮助吗?

EDITED: I have reduced my project to a very basic implementation of SPRING with Quratz Scheduler and it always loads twice. 编辑:我已经将我的项目简化为使用Quratz Scheduler的SPRING的非常基本的实现,并且它始终加载两次。 There is only one class (POJO) in the application which is called through Quartz Scheduler. 通过Quartz Scheduler调用的应用程序中只有一个类(POJO)。 The POJO prints a message on the console with the hashcode of the object and current time. POJO在控制台上显示一条消息,其中包含对象的哈希码和当前时间。 The message is printed twice for the same time with 2 different hashcodes. 该消息使用2个不同的哈希码同时打印两次。 The 2 different hashcodes hint at 2 loads of the context. 2个不同的哈希码提示了上下文的2个负载。

The project structure is given below: 项目结构如下: 在此处输入图片说明

The updated web.xml is given below (with no session listener) 更新的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>

And updated Dispatcher-Servlet xml is given below: 更新的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>

The definition of POJO which is called though Quartz Scheduler is: 通过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()));
    }

}

Any idea if its a bug in Spring jar files for this particular release? 知道这是特定版本的Spring jar文件中的错误吗?

The issue got solved for me. 这个问题为我解决了。 Its due to the Eclipse and Tomcat plugin for Eclipse. 这归功于Eclipse和Eclipse的Tomcat插件。 My project name in Eclipse was different from the ContextRoot. 我在Eclipse中的项目名称与ContextRoot不同。 Eclipse deployed the application to Tomcat once but the application is accessible through 2 URLS ie one for ProjectName and one for Context Root. Eclipse一次将应用程序部署到Tomcat,但是可以通过2个URL访问该应用程序,即一个用于ProjectName,一个用于上下文根。

1) http://localhost:8080/TractivityPhase2/login --> For project Name 1) http:// localhost:8080 / TractivityPhase2 / login- >对于项目名称

2) http://localhost:8080/Tractivity/login --> For ContextRoot 2) http:// localhost:8080 / Tractivity / login- >对于ContextRoot

So actually the application is loaded twice for the 2 URLs. 因此,实际上为2个URL加载了两次应用程序。

The issue for me got solved when I made the Context Root equal to my project name. 当我使上下文根等于我的项目名称时,对我来说问题就解决了。 Hope this answer helps someone else. 希望这个答案可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM