码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot------定时任务

时间:2018-10-21 00:57:26      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:port   bat   type   ota   nts   分享图片   启动   dem   date()   

步骤,如图所示

技术分享图片

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------定时任务

标签:port   bat   type   ota   nts   分享图片   启动   dem   date()   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!