第5章 仿写Spring的定时任务Task
在第4章 Spring 定时任务 分析Spring实现定时任务的源码。本章节是自定义注解 @EnableScheduling2、@Scheduled2 来实现定时任务的功能。本章节主要照着Spring源码进行仿写,省略一些复杂的步骤,主要用来加深源码的理解。
考虑到代码比较多,直接贴上GitHub地址:task-joy。希望喜欢的小伙伴给一些赞👍
效果
编写启动类,使用@EnableScheduling2 注解
@SpringBootApplication
@EnableScheduling2
public class TaskJoyApplication {
public static void main(String[] args) {
SpringApplication.run(TaskJoyApplication.class, args);
}
}
使用@Scheduled2注解创建一个任务
@Component
public class MyTask {
@Scheduled2(value = "task1", cron = "*/5 * * * * ?")
public void task() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ": 执行了");
}
}
运行


5176

被折叠的 条评论
为什么被折叠?



