修改application.yml
appindex:
enabled: true
创建定时任务类AppIndexTask
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Configuration
//启用定时任务
@EnableScheduling
//配置文件读取是否启用此配置 实现是通过havingValue与配置文件中的值对比,返回为true则配置类生效,反之失效
@ConditionalOnProperty(prefix = "appindex", name = "enabled", havingValue = "true")
public class AppIndexTask {
@Scheduled(cron = "0/2 * * * * ?")
public void pppIndexTask() {
try {
System.out.println("定时任务开关测试");
} catch (Exception e) {
e.printStackTrace();
}
}
}