zoukankan      html  css  js  c++  java
  • SpringBoot的ApplicationRunner和CommandLineRunner

    如果你需要在你的SpringBoot启动完成之后实现一些功能,那么可以通过创建class实现ApplicationRunner和CommandLineRunner来完成:

    @Component
    public class ApplicationRunnerTest implements ApplicationRunner {
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("========>> ApplicationRunner.run");
            for (String sourceArg : args.getSourceArgs()) {
                System.out.println(sourceArg);
            }
            for (String optionName : args.getOptionNames()) {
                System.out.println(optionName + " = " + args.getOptionValues(optionName));
            }
            System.out.println("========>> End");
        }
    }
    @Component
    public class CommandLineRunnerTest implements CommandLineRunner {
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("========>> CommandLineRunner.run");
            for (String sourceArg : args) {
                System.out.println(sourceArg);
            }
            System.out.println("========>> End");
        }
    }

    如果你定义了多个ApplicationRunner或者CommandLineRunner,并想要控制他们执行的先后顺序,可以让你定义的class实现org.springframework.core.Ordered接口,或者直接注解@Order

  • 相关阅读:
    消息中间件(MQ)
    java Lambda表达式
    【测试】性能测试及性能测试工具JMeter
    【Mysql】mysql集群方案之读写分离
    linux下mysql开启远程访问权限及防火墙开放3306端口
    MySQL事务提交与回滚
    MySQL索引
    MySQL视图
    MySQL事务
    MySQL参数化有效防止SQL注入
  • 原文地址:https://www.cnblogs.com/LOVE0612/p/9890718.html
Copyright © 2011-2022 走看看