首页 > 其他 > 详细

scheduled定时任务

时间:2019-11-26 09:36:26      阅读:102      评论:0      收藏:0      [点我收藏+]

1.scheduled定时任务类:ScheduledDemo.java

package com.nantian.scheduled;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* scheduled定时任务
*@author xjj13
*@component (把普通pojo实例化到spring容器中,相当于配置文件中的
*<bean id="" class=""/>)
*泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类
*/
@Component
public class ScheduledDemo {
/**
* 定时任务方法
* @Scheduled:设置定时任务
* 方法上添加该注解,表示方法会以定时任务的方式出现,一旦时间到达后,该方法就会被触发
* cron:(设置触发时间)cron表达式,定时任务触发是时间的一个字符表达式
*/
@Scheduled(cron="0/2 * * * * ?")//每两秒触发一次方法
public void scheduledMethod() {
System.out.println("定时器被触发"+new Date());
}
}
2.在启动类中开启定时任务的启用

springBoot中默认是对scheduled是不开启的

@SpringBootApplication
@EnableScheduling//开启scheduled定时任务
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

scheduled定时任务

原文:https://www.cnblogs.com/curedfisher/p/11933067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!