Multiple Spring boot CommandLineRunner based on command line argument

后端 未结 4 990
情话喂你
情话喂你 2020-12-31 16:11

I have created spring boot application with spring cloud task which should executes a few commands(tasks). Each task/command is shorted-lived task, and all tasks are start

4条回答
  •  星月不相逢
    2020-12-31 16:43

    You can also make your CommandLineRunner implementations @Component and @ConditionalOnExpression("${someproperty:false}")

    then have multiple profiles, that set someproperty to true to include those CommandLineRunners in the Context.

    @Component
    @Slf4j
    @ConditionalOnExpression("${myRunnerEnabled:false}")
    public class MyRunner implements CommandLineRunner {
        @Override
        public void run(String ... args) throws Exception {
            log.info("this ran");
        }
    }
    

    and in the yml application-myrunner.yml

    myRunnerEnabled: true
    
    @SpringBootApplication
    public class SpringMain {
        public static void main(String ... args) {
            SpringApplication.run(SpringMain.class, args);
        }
    }
    

提交回复
热议问题