zoukankan      html  css  js  c++  java
  • 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启动,查看打印日志即可

  • 相关阅读:
    226_翻转二叉树
    199_二叉树的右视图
    145_二叉树的后序遍历
    做IT,网络/系统/数据库/软件开发都得懂
    [恢]hdu 1200
    [恢]hdu 2080
    [恢]hdu 1222
    [恢]hdu 1128
    [恢]hdu 2153
    [恢]hdu 2132
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/9823507.html
Copyright © 2011-2022 走看看