springboot定时任务

1、主程序添加注解

import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling

 

2、定时任务

可在主程序添加方法,也可在service类添加方法

package com.xiaostudy.shiro_test1.service;

import com.xiaostudy.shiro_test1.utils.BackupMysqlUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: xiaostudy
 * Date: 2019/7/25
 * Time: 22:40
 * Description: No Description
 */
@Service
public class ScheduledService {

    /**
     * 定时备份mysql,每天22:42备份
     */
    @Scheduled(cron = "0 42 22 * * ?")
    public void timingBackupMysql() {
        BackupMysqlUtils.backup();
    }
}

 

@Scheduled参数及cron表达式解释

上一篇:Spring Boot中使用@Scheduled创建定时任务


下一篇:Vue重新安装测试hello1项目(一看就懂)