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 的解决方案)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM