1. application.xml 引入命名空间 :
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
2.定时任务类
package com.yuan.other;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//spring 定时任务
@Component("taskJob")
public class TaskJob {
@Scheduled(cron = "0 0/1 * * * ?") ----- 每秒触发一次
public void job(){
System.out.println("起床啦...");
}
}
3. application.xml 开启定时任务
<!-- 开启这个配置,spring才能识别@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
完毕、
==========================================================================
不想引入命名空间之类的配置,只需要直接在代码中写:
@Component
@EnableScheduling
public class TaskJob {
@Scheduled(cron = "0 0/1 * * * ?") ----- 每秒触发一次
public void job(){
System.out.println("起床啦...");
}
}