spring task 实现定时执行(补充:解决定时任务执行2次问题)

首先在spring-mvc.xml配置头文件引入:

xmlns:task="http://www.springframework.org/schema/task"

其次引入task对应的xsi:schemaLocation:

http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task.xsd

然后确保spring进行组件扫描时涵盖定时任务类所在的包:

<context:component-scan base-package="xx.xx.xx" />

最后设置任务类即可:

import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
@Lazy(value=false)
public class MyQuartzs {

    @Scheduled(cron = "0 0 0 * * ?")//零点时执行一次
    public void test() {
        //TODO
    }
}

至于@Scheduled(cron="0 0 0 * * ?")中cron的意思,请看我的另一篇文章。

http://www.cnblogs.com/sprinkle/p/6440018.html

补充:解决定时任务执行2次问题

请看:

Spring3 Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)

原文地址:https://www.cnblogs.com/sprinkle/p/6440050.html