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表达式解释