Spring Boot @EnableScheduling conditionally

前端 未结 4 1765
慢半拍i
慢半拍i 2021-02-20 03:53

Is there a way to make @EnableScheduling conditional based on an application property? also is it possible to disable controllers based on properties too?

What I\'m tryi

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-20 04:29

    1. I think scheduler is not configuration.
    2. No need to set prefix in @ConditionalOnProperty
    3. No need to create scheduler over @Bean in configuration class.

    My variant:

    @Component
    @EnableScheduling
    @ConditionalOnProperty(value = "scheduler.enabled", havingValue = "true")
    public class MyScheduler {
    
        @Scheduled(...)
        public void doWork() {
            ...
        }
    }
    
    

提交回复
热议问题