如果配置內容不在application.yml或application.properties,則需要在類上注解
@PropertySource(value = "classpath:task.properties",encoding = "UTF-8")
具體執行的方法上寫這個
@Scheduled(cron="${Task_expireOTCAdsState_cron}")
import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.PropertySource; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @PropertySource(value = "classpath:task.properties",encoding = "UTF-8") public class QuartzService { @Autowired private OTCAdsService otcAdsService; /** * 定時任務更新廣告過期 */ @Scheduled(cron="${Task_expireOTCAdsState_cron}") // @Scheduled(cron = "0 0/1 * * * ?") public void expireOTCAdsState(){ LogUtils.info(this.getClass(), "廣告過期執行開始:"); LogUtils.info(this.getClass(), "廣告過期執行行數:" + count); LogUtils.info(this.getClass(), "廣告過期執行結束:"); } }
task.properties
# 任務執行時間cron配置
Task_expireOTCAdsState_cron=0 0/1 * * * ?