springboot定时任务@scheduled 不同环境下配置开关

    xiaoxiao2022-07-05  182

    1.添加配置类 SchedulingConfig.java

    package com.duan.video.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; /** * 定时任务开关 * @author duanjw */ @Configuration //启用定时任务 @EnableScheduling //配置文件读取是否启用此配置 @ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true") public class SchedulingConfig { }

     

    2. 在application.yaml添加开关定时的配置

    spring: ## 默认使用dev配置 profiles: active: dev --- ## 开发的配置 spring: profiles: dev --- ## 生产的配置 spring: profiles: prd ## 开启定时任务 scheduling: enabled: true

    3. 直接启动会使用默认的dev配置,生产环境在启动命令上指定prd配置:java -jar my.jar --spring.profiles.active=prd

    最新回复(0)