Spring框架开启自带的任务调度器执行任务(注解方式)
编程教程
>
Java
>
Spring
(8774)
2024-11-26 14:39:04
1.首先需要在配置中开启spring框架自带的任务调度器,开启代码如下
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling
public class SpringScheduleConfig {
}
2.在需要使用的地方使用注解@Scheduled来配置任务的具体调度时间等
例如:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TestTask {
@Scheduled(cron = "00 30 02 * * ?") // 每天02:30分执行博客自动审核任务
public void deleteUnUse() {
System.out.println("do somthing....");
}
}
http://blog.xqlee.com/article/75.html