示例
1. 获取配置bean
注意使用配置动态刷新注解@RefreshScope.
@Data
@RefreshScope
@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties {
private String filesPath;
private String directoryCleanCron;
}
2. 实现定时任务的代码
注意:实现定时任务需要使用@EnableScheduling和@Scheduled搭配使用。
@Slf4j
@RefreshScope
@Component
@EnableScheduling
public class FileCleanSchedule implements ApplicationListener<RefreshScopeRefreshedEvent>{
@Resource
private TestProperties testProperties;
@Scheduled(cron = "#{testProperties.getDirectoryCleanCron()}")
public void clean(){
log.info("[文件夹清理]-start");
String filesPath = null;
try {
filesPath = testProperties.getFilesPath();