spring 3.0后实现了本身的定时任务工具 只要引入spring包就能够 使用起来也比较简单html
咱们来看看怎样使用spring
首先在spring头文件加入命名空间 xmlns:taskspring-mvc
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd" >
开启注解扫描mvc
<context:component-scan base-package="com.dlh.*" />
开启 task注解支持工具
<!--开启这个配置,spring才能识别@Scheduled注解 --> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/>
实体类code
public class TaskJob { @Scheduled(cron = "3 * * * * ?") public void job(){ System.out.println("每分钟3秒的时候执行一次"); } @Scheduled(cron ="0/3 * * * * ?") public void job1(){ System.out.println("每3秒执行一次"); } }
编译项目后运行结果component
解析下这个corn表达式xml
cron = "3 * * * * ?" //每分钟的3秒执行 1分03秒 2分03秒都会执行 cron ="0/3 * * * * ?" //每隔3秒都会执行一次
Corn表达式htm
corn表达式有7个参数(至少须要6个 年份条件可不写) 参数表示调度任务做用的时间 分别表明blog
{秒}{分}{时}{日期}{月份}{星期}{年份}
参数 用空格隔开
"0 15 10 * * ?" //天天的10:15执行 0表明秒的条件 15 表明分的条件 10表明小时的条件
关于corn更具体的解释 这篇文章比较详细