SpringBoot(31)定时任务

1.开启定时功能

因为定时任务是异步任务,只能部署在程序入口。

@EnableScheduling
@SpringBootApplication
public class SpringbootAsynchronousApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootAsynchronousApplication.class, args);
	}

}

2.编写定时任务

使用了cron表达式。
crontab文件的格式:M H D m d cmd.
M: 分钟(0-59)。
H:小时(0-23)。
D:天(1-31)。
m: 月(1-12)。
d: 一星期内的天(1~7,1为星期天,7为星期六)。


@Service
public class ScheduledService {

	//表示每隔三秒执行一次
    @Scheduled(cron = "0/3 * * * * ?")
    public void hello(){

        System.out.println("宝贝你被执行了。。。。。。。");
    }
}

你可能感兴趣的:(SpringBoot,spring,boot)