SpringBoot------定时任务

步骤,如图所示

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启动,查看打印日志即可

 

posted @ 2018-10-20 23:40  玉天恒  阅读(174)  评论(0编辑  收藏  举报