首页 > 编程语言 > 详细

SpringBoot------定时任务

时间:2018-10-21 00:32:37      阅读:186      评论:0      收藏:0      [点我收藏+]

步骤,如图所示

技术分享图片

1.添加定时任务1业务类

package top.ytheng.demo.task;

import java.util.Date;

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

//定时任务业务类
@Component
public class TestTask {

    //两秒执行一次
    @Scheduled(fixedRate=2000)
    public void sum() {
        System.out.println("当前时间:" + new Date());
    }
}

2.添加启动类

package top.ytheng.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication //等于下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//拦截器用到
@ServletComponentScan
//MyBatis用到
@MapperScan("top.ytheng.demo.mapper")
//定时使用(开启定时任务)
@EnableScheduling
public class DemoApplication {

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

3.右键项目Run As启动,查看打印日志即可

 

SpringBoot------定时任务

原文:https://www.cnblogs.com/tianhengblogs/p/9823507.html

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