CommandLineRunner或者ApplicationRunner接口

最近學(xué)習(xí)有些急躁,定下心來。

前言

CommandLineRunner、ApplicationRunner 接口是在容器啟動(dòng)成功后的最后一步回調(diào)(類似開機(jī)自啟動(dòng))。

CommandLineRunner接口

CommandLineRunner

官方doc:
Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or Order @Order annotation.
接口被用作將其加入spring容器中時(shí)執(zhí)行其run方法。多個(gè)CommandLineRunner可以被同時(shí)執(zhí)行在同一個(gè)spring上下文中并且執(zhí)行順序是以order注解的參數(shù)順序一致。

If you need access to ApplicationArguments instead of the raw String array
consider using ApplicationRunner.
如果你需要訪問ApplicationArguments去替換掉字符串?dāng)?shù)組,可以考慮使用ApplicationRunner類。

先看一個(gè)demo:
定義一個(gè)ServerStartedReport實(shí)現(xiàn)CommandLineRunner,并納入到srping容器中進(jìn)行處理

package com.zhihao.miao;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Order(2)
@Component
public class ServerStartedReport implements CommandLineRunner{
    @Override
    public void run(String... args) throws Exception {
        System.out.println("===========ServerStartedReport啟動(dòng)====="+ LocalDateTime.now());
    }
}

定義一個(gè)ServerSuccessReport實(shí)現(xiàn)CommandLineRunner,并納入到spring容器處理

package com.zhihao.miao;


import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Order(1)
@Component
public class ServerSuccessReport implements CommandLineRunner{
    @Override
    public void run(String... args) throws Exception {
        System.out.println("=====應(yīng)用已經(jīng)成功啟動(dòng)====="+ Arrays.asList(args));
    }
}

啟動(dòng)類測試,也可以直接在spring容器訪問該值,

package com.zhihao.miao;


import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context =SpringApplication.run(Application.class,args);
        ApplicationArguments applicationArguments = context.getBean(ApplicationArguments.class);
        System.out.println("============");
        System.out.println("name="+applicationArguments.getOptionNames());
        System.out.println("values===="+applicationArguments.getOptionValues("developer.name"));
    }
}

配置參數(shù),然后執(zhí)行啟動(dòng)類


圖片.png

打印結(jié)果


ApplicationRunner接口

發(fā)現(xiàn)二者的官方j(luò)avadoc一樣,區(qū)別在于接收的參數(shù)不一樣。CommandLineRunner的參數(shù)是最原始的參數(shù),沒有做任何處理。ApplicationRunner的參數(shù)是ApplicationArguments,是對原始參數(shù)做了進(jìn)一步的封裝。

ApplicationArguments是對參數(shù)(main方法)做了進(jìn)一步的處理,可以解析--name=value的,我們就可以通過name來獲取value(而CommandLineRunner只是獲取--name=value)

可以接收--foo=bar這樣的參數(shù)。

--getOptionNames()方法可以得到foo這樣的key的集合。
--getOptionValues(String name)方法可以得到bar這樣的集合的value。

看一個(gè)demo:
定義MyApplicationRunner類繼承ApplicationRunner接口,

package com.zhihao.miao;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class MyApplicationRunner implements ApplicationRunner{

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("===MyApplicationRunner==="+ Arrays.asList(args.getSourceArgs()));
        System.out.println("===getOptionNames========"+args.getOptionNames());
        System.out.println("===getOptionValues======="+args.getOptionValues("foo"));
        System.out.println("==getOptionValues========"+args.getOptionValues("developer.name"));
    }
}

啟動(dòng)類,

package com.zhihao.miao;


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

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

配置參數(shù)啟動(dòng),


打印結(jié)果:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,641評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,279評論 6 342
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,740評論 18 399
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,504評論 2 7
  • 任它桎梏于心 糜爛于口 我終將不提 只記來日方長 憑細(xì)水長流 你的名字刻在誰的心房 誰的名字又...
    女二號妖精閱讀 471評論 3 4

友情鏈接更多精彩內(nèi)容