①:配置pom.xml
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
②:spring核心配置文件 applicationContext.xml加入配置
头部加入:
xmlns:task="http://www.springframework.org/schema/task"
xsi:http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
具体配置加入:
<task:annotation-driven/>
③:在需要定时的方法上加入@Scheduled
@Scheduled(cron = "5 5/30 * * * *")//从第五分钟开始,每三十分钟的第五秒执行一次
public void quartzJobTestMethod() {
System.out.println("定时任务执行:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
④:main方法
@EnableScheduling//开启基于注解的定时任务
public class YaKiTimedTask {
private static ApplicationContext context;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
context.getBean(OrderUtils.class);//注入定时任务所在的类
}
}
网友评论