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

  • 相关阅读:
    js正则
    常用正则表达式
    JS
    Vue
    JS
    Cookie、Session和自定义分页
    ORM分组操作示例(与SQL语句的比较)以及基于对象和queryset的正反查询
    跨站请求伪造和csrf_token使用
    ORM之单表、多表操作
    Django中ORM介绍和字段及字段参数
  • 原文地址:https://www.cnblogs.com/LOVE0612/p/9890718.html
Copyright © 2011-2022 走看看