简体   繁体   中英

Spring 4 runs schedule task twice, no applicationContext.xml

I read both of this but they do not seem to work

Java Spring @Scheduled tasks executing twice

Spring @Scheduled is executing task twice when using annotations

I do not have @Configurable class, and I do not use applicationConetxt.xml .

My ScheduleTasks

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

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

I can see that when I run the app in tomcat, the task prints twice evrey 5 seconds.

Somebody says to remove

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

it works , but I feel this is not a solution. since this should be needed. (at least from the example from google)

Here is my 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>

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"
    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>

Remove the context loader listener. You only need that if you have to initialize a parent context. The contexts from the dispatcher servlet are standalone, but could access such parent contexts if needed, ie to retreive a service or dao.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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