spring 自带的定时器task

本文介绍了如何在Spring框架中配置和实现定时任务,包括在配置文件中启用定时任务支持、使用注解定义定时任务以及通过XML配置定时任务的具体方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、在spring的配置文件中加入task配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:task="http://www.springframework.org/schema/task"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	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-3.0.xsd">

	<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
	<context:component-scan base-package="com.spring.demo" />
	
	<task:annotation-driven />  <!-- 定时器开关 -->

</beans>



二、使用注解,对task类进行注入:

package com.spring.demo.task;

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

@Component
public class Task {

	@Scheduled(cron = "0/1 * * * * ?")
	public void doCheck() {
		System.out.println(1);
	}

}


三、二中的定时任务每隔1秒会打印一次,如果定时任务多的话,可以单独写个配置文件对定时任务进行管理:

package com.spring.demo.task;

import org.springframework.stereotype.Component;

@Component
public class Task {

	public void doCheck() {
		System.out.println(1);
	}

}

改成使用spring-task.xml配置文件,别忘了要记得加载这个配置文件哦:

<?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-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
	default-lazy-init="true">

	<task:scheduled-tasks>
		<task:scheduled ref="task" method="doCheck" cron="0/2 * * * * ?" />
	</task:scheduled-tasks>


</beans>

定时配置:

参考地址:http://www.blogjava.net/bolo/archive/2015/03/12/423408.html

Cron表达式包括下面7个字段并区别顺序0-590-59小时0-23月内日期1-311-12或者JAN-DEC周内日期1-7或者SUN-SAT(可选字段)留空或者1970-2099并且通过特殊字符表示特殊意义具体为下
    斜线(/)字符表示增量值例如在秒字段中"5/15"代表从第5秒开始15秒一次
    问号(?)字符和字母L字符只有在月内日期和周内日期字段中可用问号表示这个字段不包含具体值所以如果指定月内日期可以在周内日期字段中插入"?"表示周内日期值无关紧要这里有个很蛋疼的设定,无关Quartz,而是Spring集成Quartz后,它自己加的一个约束,那就是:日期(1-31)和星期(SUN-SAT)两者,必须有一个是问号(?),系统在启动的时候,Spring会检查表达式,如果不符合它的规则,就会抛异常。所以在使用的时候这个地方一定要注意,而这个在Linux上执行Cron是没有这个限制的。
    字母L字符是last的缩写放在月内日期字段中表示安排在当月最后一天执行在周内日期字段中如果"L"单独存在,就等于"7"否则代表当月内周内日期的最后一个实例所以"0L"表示安排在当月的最后一个星期日执行
    字母(W)字符把执行安排在最靠近指定值的工作日"1W"放在月内日期字段中表示把执行安排在当月的第一个工作日内
    井号(#)字符为给定月份指定具体的工作日实例"MON#2"放在周内日期字段中表示把任务安排在当月的第二个星期一
    星号(*)字符是通配字符,表示该字段可以接受任何可能的值表达式例子。
    例子:
    "0 0 08 * * ?" 每天上午8点触发
    "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期间的每1分钟触发
    "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
    "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
    "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
    "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:102:44触发
    "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
    "0 15 10 15 * ?" 每月15日上午10:15触发
    "0 15 10 L * ?" 每月最后一日的上午10:15触发
    "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
    "0 15 10 ? * 6L 2009-2019" 2009年至2019年的每月的最后一个星期五上午10:15触发
    "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值