前言:quartz有简单任务和定时任务,这里只写定时任务,采用注解方式工作常用
万事第一步引包
shiro所需的包
<!-- quartz核心 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.3</version>
</dependency>
修改spring的主配置文件
在主配置文件中文件头添加
xmlns:末尾加上
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation加上
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
添加注解扫描包:
<!-- 配置注解扫描器 @Service -->
<context:component-scan base-package="com.dxsysg.service"></context:component-scan>
多个包逗号隔开
开启注解驱动
<task:annotation-driven/>
测试
编写quartzService 使用 @Scheduled 注解
package com.dxsysg.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @author zhaozeren
* @version 1.0
*/
@Service
public class quartzService {
@Scheduled(cron = "0/5 * * * * ? ")
public void quartz(){
System.out.println("定时任务开起");
}
}
@Scheduled(cron = "0/5 * * * * ? ")
每5秒执行一次,cron表达式可在线生成 cron在线表达式
启动项目: