zoukankan      html  css  js  c++  java
  • springboot 中的commandLineRunners接口

    首先看实现了两个接口运行的顺序结果:

    My1:

    package com.example.commandlinerunner;

    import lombok.extern.java.Log;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.core.annotation.OrderUtils;
    import org.springframework.stereotype.Component;

    import java.util.Arrays;

    /**
    * Created by WangYx on 2017/9/14.
    */
    @Component
    @Order(6)
    @Log
    public class MyCmdLinerRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
    log.info("MyCmdLinerRunner1:order = " + OrderUtils.getOrder(this.getClass())+" :args = "
    + Arrays.toString(args));
    }

    private String getStacks(){
    StringBuilder sb = new StringBuilder();
    StackTraceElement[] elements = Thread.currentThread().getStackTrace();
    sb.append("============= ");

    for (int i = 0 ; i < elements.length ; i++){
    sb.append(elements[i]).append(" ");
    }
    return sb.toString();
    }
    }

    My2:

    package com.example.commandlinerunner;

    import lombok.extern.java.Log;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.core.annotation.OrderUtils;
    import org.springframework.stereotype.Component;

    import java.util.Arrays;

    /**
    * Created by WangYx on 2017/9/14.
    */
    @Component
    @Order(5)
    @Log
    public class MyCmdLinerRunner2 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
    log.info("MyCmdLinerRunner2:order = " + OrderUtils.getOrder(this.getClass())+" :args = "
    + Arrays.toString(args));
    }


    }

    主函数:
    package com.example.commandlinerunner;
    /**
    * 通过CommandLineRunner,可在所有Spring Bean和 ApplicationContext被创建后执行一些可以访问命令行参数的任务。
    * 如想指定多个CommandLineRunnerBean的执行顺序,
    * 可以实现org.springframework.core.Ordered接口
    * 或添加org.springframework.core.annotation.Order注解
    */

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class CommandlinerunnerApplication {

    public static void main(String[] args) {
    SpringApplication.run(CommandlinerunnerApplication.class, args);
    }
    }

    /**
    * org.springframework.core.annotation.AnnotationAwareOrderComparator负责对CommandLineRunnerBean进行排序。排序规则为:
    如果有一方是org.springframework.core.PriorityOrdered接口实现,而另一方不是,则PriorityOrdered接口实现一方获胜;
    检查org.springframework.core.Ordered接口或 org.springframework.core.annotation.Order 注解获得order,值小者胜;
    其他没有order的则置为Ordered.LOWEST_PRECEDENCE,顺序不定。
    在上述测试中,MyCmdLineRunner2的order为5,MyCmdLineRunner1的order为6,因此MyCmdLineRunner2在MyCmdLineRunner1之前执行。
    Application的demo1和demo2方法设置了@order注解,但是调试可知lamda表达式生成类并没有@order注解信息,因此执行顺序排在后面。
    这是需要注意的地方。此外,Bean初始化顺序跟CommandLineRunner执行顺序也没有关系。
    */


  • 相关阅读:
    学了axure的感受
    axure的功能
    PS的应用
    day15-1 模块
    day14-2 模块详解
    day14-1 模块定义,导入
    day13-1 Json & pickle 数据序列化
    day12-3 内置方法
    day12-2 内置方法
    day12-1 迭代器和生成器
  • 原文地址:https://www.cnblogs.com/wyx1202/p/7519976.html
Copyright © 2011-2022 走看看