1、启动类添加注解
@EnableScheduling
2、创建定时任务类
@Component
public class SchedulerTask {
private int count =0;
@Scheduled(cron = "*/6 * * * * ?")
private void task(){
System.out.println("this is scheduler task runing "+(count++));
}
}
3、运行项目成功图
4、注解详解
定时任务注解接口内部构造:
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
String cron() default "";
String zone() default "";