yml文件-动态开关

本质:${} 读取字符串

方案一

增加一个属性swith来判断业务流走向

//test.swith是配置文件中定义的参数
@value("${test.swith}") 
String swith;

public  void func() {
    if("on".equals(swith)){
    //执行对应的定时任务代码
    }
}

 

方案二

通过@ConditionalOnProperty注解来决定是否注入spring容器

@Component
@EnableScheduling
@ConditionalOnProperty(name = "timer.enabled")
public class ConditionalOnPropertyTest {

    @Scheduled(cron = "*/5 * * * * ?")
    public void test(){
        logger.info("定时器执行....");
    }
}

 

 

参考文章

【1】@ConditionalOnProperty注解用法

posted @ 2024-03-22 13:56  先娶国王后取经  阅读(6)  评论(0编辑  收藏  举报