2014-11-06 59 views
1

我看两者的这一点,但他们似乎没有工作弹簧4个运行计划任务的两倍,没有applicationContext.xml中

Java Spring @Scheduled tasks executing twice

Spring @Scheduled is executing task twice when using annotations

我没有@Configurable类,和我不要使用applicationConetxt.xml

我ScheduleTasks

@EnableScheduling 
@Component 
public class ScheduledTasks { 
    @Autowired 
    private ApplicationContext ctx; 

    @Scheduled(fixedRate=5000) 
    public void monitorRoom(){ 
     System.out.println("--------------------"); 
    } 
} 

我可以看到,当我运行在Tomcat中的应用程序,任务打印两次evrey 5秒。

有人说,除去

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

它的工作原理,但我觉得这不是一个解决方案。因为这应该是需要的。(在从谷歌的例子至少)

这是我的web.xml

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <display-name>wodinow</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.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"> 

    <context:component-scan base-package="wodinow.weixin.jaskey" /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/jsp/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

    <mvc:resources mapping="/pages/**" location="/resources/pages/" /> 
    <mvc:resources mapping="/images/**" location="/resources/images/" /> 

    <mvc:annotation-driven/> 

</beans> 
+0

如果您正在使用xml文件进行配置,则名称无关紧要。 'ContextLoaderListener'和'DispatcherServlet' xml都加载同一个文件,这意味着每个bean都有两次,包括你的预定的一个。简单的解决方案从您的配置中删除'ContexLoaderListener'。另一件事使选择使用XML或Java配置,但不要混合。你有一个'@ EnableScheduling',它是基于java的配置,你使用的是xml,而不是那个注解,在你的xml配置中添加''。 – 2014-11-06 12:34:34

+0

@ M.Deinum,谢谢!因此,对于加载dispatcher-servlet.xml的任何情况,我们不需要使用contextloaderlistener?为什么所有的指南都给了我相同的配置?你能否在答案中发布你的答案?谢谢! – Jaskey 2014-11-06 12:39:29

回答

1

删除上下文加载器监听器。如果您必须初始化父上下文,则只需要这样做。来自调度程序servlet的上下文是独立的,但如果需要可以访问这样的父上下文,即,可以检索服务或道。

+0

所以,我可以考虑如果我使用弹簧mvc,这个上下文加载不需要? – Jaskey 2014-11-06 12:44:27