springboot整合springTask

全他吗被取了 / 2023-05-07 / 原文

一、启动类开启task注解

//springTask定时任务开启
@EnableScheduling
@SpringBootApplication
@MapperScan("com.zhenghe.mapper")
public class ZhengheApplication {
    public static void main(String[] args) {

        SpringApplication.run(ZhengheApplication.class,args);
    }
}

二、编写实际要执行的定时任务

@Component
public class TestTask {

    @Scheduled(cron = "0/5 * * * * *")
    private void test(){
        //这里是要写实际执行的定时任务业务
        System.out.println("定时任务开启:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

}

三、启动测试