springboot 中的commandLineRunners介面

來源:互聯網
上載者:User

標籤:多個   產生   figure   順序   資訊   獲得   exce   lamda   autoconf   

首先看實現了兩個介面啟動並執行順序結果:

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("=============\n");

for (int i = 0 ; i < elements.length ; i++){
sb.append(elements[i]).append("\n");
}
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執行順序也沒有關係。
*/


springboot 中的commandLineRunners介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.