Spring Boot @EnableScheduling conditionally

前端 未结 4 1773
慢半拍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:19

    You can annotate a bean injection like this:

    @Bean
    @ConditionalOnProperty(prefix = "your.property", name = "yes", matchIfMissing = true)
    public void doSomeBackendJob() {
           /* job implementation here */
    }
    

    Bonus: Since you want to run different things in different machines, i.e., you will deploy the same app with different configurations, you could use spring profiles, if that's the case you can annotate a class or method like this:

    @Component
    @Profile({ Constants.SPRING_PROFILE_PRODUCTION, Constants.SPRING_PROFILE_TEST })
    public class TheClass{...}
    

提交回复
热议问题