springtask 的使用方法和 cron 表达式解析
作者:魔有追求 时间:2023-06-19 20:05:56
spring 容器依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
开启任务注解驱动。即扫描的时候扫描 springtask 相关的注解。
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd
">
<context:component-scan base-package="com.mozq.task"/>
<task:annotation-driven/>
</beans>
准备 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);
}
}
参考文档
https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/integration.html#scheduling
来源:https://www.cnblogs.com/mozq/p/11341901.html
标签:spring,task,用法,cron,表达式
0
投稿
猜你喜欢
Android调用系统默认浏览器访问的方法
2022-04-02 12:14:52
android dialog背景模糊化效果实现方法
2023-09-17 02:31:23
java base64编码、解码的三种方式总结
2022-12-29 22:20:51
C#利用时间和随即字符串创建唯一的订单编号
2022-01-21 15:18:56
C#如何用ThoughtWorks生成二维码
2022-09-28 20:44:53
java垃圾回收原理之GC算法基础
2023-10-05 16:10:09
CentOS安装jdk的三种方法
2022-01-13 06:24:41
Android中 webView调用JS出错的解决办法
2021-12-02 21:23:25
Android利用二阶贝塞尔曲线实现添加购物车动画详解
2022-03-01 09:49:49
浅谈@FeignClient中name和value属性的区别
2023-11-06 13:04:14
windows 部署JAVA环境安装iDea的详细步骤
2022-10-09 01:09:26
Java 运算符详情
2022-02-12 05:54:43
android点击无效验证的解决方法
2022-02-28 03:55:08
kotlin基础教程之类和继承
2023-09-24 07:55:53
c#之滚动字幕动画窗体的实现详解
2022-02-14 08:46:19
Java 异步实现的几种方式小结
2022-09-23 00:26:56
SpringBoot利用@Retryable注解实现接口重试
2023-03-08 19:54:42
WinForm窗体间传值的方法
2023-12-17 08:53:08
java + dom4j.jar提取xml文档内容
2023-11-29 03:55:10
Java monitor机制使用方法解析
2023-11-09 11:25:56