@Schedule cron 表达式写入配置中

将定时任务时间表达式 @Schedule cron 写入配置中,方便我们进行修改

 

yml 中进行配置

taskclearFile:  '0 0/1 * * * ?'

定时任务中使用 ${taskclearFile} 进行配置

package com.musicmanager.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ServerFileTask {

    @Scheduled(cron = "${taskclearFile}")
    public void fileScan(){
       System.out.println("执行了定时任务");
    }
}

直接写到 cron 配置中

package com.che.pri.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TestTask {

	@Scheduled(cron = "0 0/1 * * * ?")
	public void testTask() {
		System.out.println("执行了定时任务");
	}
}	

 

你可能感兴趣的:(spring,boot)