这篇文章主要介绍了springtask 的使用方法和 cron 表达式解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring 容器依赖
org.springframework spring-context 5.0.5.RELEASE
开启任务注解驱动。即扫描的时候扫描 springtask 相关的注解。
准备 Spring 容器 + 定时任务
为了使用 springtask 需要准备 spring 容器和定时任务。通过 main 方法创建 spring 容器。@Scheduled 注解创建定时任务。
package com.mozq.task; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class TaskDemo { @Scheduled(cron="* * * * * ?") public void sendOrderMessage(){ System.out.println("发送订单消息"); } public static void main(String[] args) throws InterruptedException { ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring-task.xml"); Thread.sleep(1000); } }
参考文档
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。
以上就是springtask 的使用方法和 cron 表达式解析的详细内容,更多请关注0133技术站其它相关文章!