最新消息: 关于Git&GitHub 版本控制你了解多少?
您现在的位置是:群英 > 开发技术 > Python语言 >
pring框架实现定时任务调度如何操作?
网络发表于 2020-09-04 18:06 次浏览

pring框架实现定时任务调度如何操作?


应用场景

一般用于定时发工资
或者定时清理垃圾
周期性更新数据库

使用

先导入包
在这里插入图片描述

第一步:
在Spring配置文件中进行配置基本信息
在这里插入图片描述
第二步:在xsi中添加

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

在这里插入图片描述
第三步:然后是必须的一项,因为这个是spring提供的定时任务,所以需要进行扫描类下的包才能执行相应的操作 配置扫描的包 我把相应的测试类放到com.test包下,这个一定要写
在这里插入图片描述

第四步: 需要执行的定时任务

1. import org.springframework.scheduling.annotation.Scheduled;
2. import org.springframework.stereotype.Component;
3.  
4. /**
5.  * Created by leo on 2018/1/22.
6.  */
7. @Component
8. public class TimerTask {
9.    
10.     @Scheduled(cron = "* * * * * ?")//每分钟都执行
11.     public void test(){
12.         System.out.println("执行");
13.     }
14.  
15.    
16. }

在这里插入图片描述
Cron配置 CRON表达式 含义 “0 0 12 * * ?” 每天中午十二点触发 “0 15 10 ? * *” 每天早上10:15触发 “0 15 10 * * ?” 每天早上10:15触发 “0 15 10 * * ? *” 每天早上10:15触发 “0 15 10 * * ? 2005” 2005年的每天早上10:15触发 “0 * 14 * * ?” 每天从下午2点开始到2点59分每分钟一次触发 “0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发 “0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 “0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发 “0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发 “0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发 第五步:测试

在这里插入图片描述

package com.ath.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ath.spring.beans.autowired.Person;

public class Test {
	
	public static void main(String[] args) {
		ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
		//没有主动调用 test方法
		
	}

}

步骤 任务调度:

  1. 搭建spring 框架

  2. 添加jar包

  3. aopalliance 任务调度需要的jar包

  4. 写要执行的方法

  5. 设置执行的时间

  6. 配置文件中添加 <task:annotation-driven/>

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
相关信息推荐
2021-01-15 15:02:09 关键词:spring是什么意思
摘要:Spring Boot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。接下来通过本文给大家介绍Spring与Spring boot的区别介绍,非常不错,需要的朋友参考下吧。 什么是S..