springboot項目啓動成功後執行一段代碼的兩種方式

springboot項目啓動成功後執行一段代碼的兩種方式

 

實現ApplicationRunner接口

package com.lnjecit.lifecycle;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author lnj
 * createTime 2018-11-07 22:37
 **/
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("經過實現ApplicationRunner接口,在spring boot項目啓動後打印參數");
        String[] sourceArgs = args.getSourceArgs();
        for (String arg : sourceArgs) {
            System.out.print(arg + " ");
        }
        System.out.println();
    }
}

 

項目啓動後,會打印以下信息:java

 

 

實現CommandLineRunner接口

package com.lnjecit.lifecycle;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author lnj
 * createTime 2018-11-07 22:25
 **/
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("經過實現CommandLineRunner接口,在spring boot項目啓動後打印參數");
        for (String arg : args) {
            System.out.print(arg + " ");
        }
        System.out.println();
    }
}

 

 

 兩種實現方式的不一樣之處在於run方法中接收的參數類型不同git

 

指定執行順序

當項目中同時實現了ApplicationRunner和CommondLineRunner接口時,可以使用Order註解或實現Ordered接口來指定執行順序,值越小越先執行github

 

原理

經過調試能夠發現都是在 org.springframework.boot.SpringApplication#run(java.lang.String...)方法內的afterRefresh(上下文刷新後處理)方法時,會執行callRunners方法。spring

afterRefresh方法具體實現以下: springboot

protected void afterRefresh(ConfigurableApplicationContext context,
        ApplicationArguments args) {
    callRunners(context, args);
}

callRunners方法具體實現以下: ide

private void callRunners(ApplicationContext context, ApplicationArguments args) {
    List<Object> runners = new ArrayList<Object>();
    runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
    runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
    AnnotationAwareOrderComparator.sort(runners);
    for (Object runner : new LinkedHashSet<Object>(runners)) {
        if (runner instanceof ApplicationRunner) {
            callRunner((ApplicationRunner) runner, args);
        }
        if (runner instanceof CommandLineRunner) {
            callRunner((CommandLineRunner) runner, args);
        }
    }
}

 能夠看出上下文完成刷新後,依次調用註冊的runners, runners的類型爲 ApplicationRunner 或 CommandLineRunnerspa

 

案例地址

https://github.com/linj6/springboot-learn/tree/master/springboot-runner.net

 

參考資料

http://www.javashuo.com/article/p-hsvrdiyq-ce.html調試

相關文章
相關標籤/搜索