国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - spring boot 命令行啟動(dòng)的方式

spring boot 命令行啟動(dòng)的方式

2021-07-22 15:57posuoren Java教程

這篇文章主要介紹了spring boot 命令行啟動(dòng)的方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在使用spring boot 構(gòu)建應(yīng)用啟動(dòng)時(shí),我們?cè)诠ぷ髦卸际峭ㄟ^命令行來啟動(dòng)應(yīng)用,有時(shí)候會(huì)需要一些特定的參數(shù)以在應(yīng)用啟動(dòng)時(shí),做一些初始化的操作。

spring boot 提供了 commandlinerunner 和 applicationrunner 這兩個(gè)接口供用戶使用。

1. commandlinerunner

1.1 聲明:

?
1
2
3
4
5
6
7
8
9
10
11
@functionalinterface
public interface commandlinerunner {
 
  /**
   * callback used to run the bean.
   * @param args incoming main method arguments
   * @throws exception on error
   */
  void run(string... args) throws exception;
 
}

1.2 使用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.consoleapplication;
 
import org.springframework.boot.commandlinerunner;
import org.springframework.stereotype.component;
 
@component
public class testrunner implements commandlinerunner {
 
  @override
  public void run(string... args) {
    // do something...
    for(string arg: args){
      system.out.println(arg);
    }
    system.out.print("test command runner");
  }
}

1.3 運(yùn)行結(jié)果

運(yùn)行: java -jar build/libs/consoleapplication-0.0.1-snapshot.jar -sdfsaf sdfas,

結(jié)果如下:

2019-03-16 17:31:56.544  info 18679 --- [           main] c.e.consoleapplication.demoapplication   : no active profile set, falling back to default profiles: default
2019-03-16 17:31:57.195  info 18679 --- [           main] c.e.consoleapplication.demoapplication   : started demoapplication in 16.172 seconds (jvm running for 16.65)
-sdfsaf
sdfas
test command runner%

2. applicationrunner

2.1 聲明

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link springapplication}. multiple {@link applicationrunner} beans can be defined
 * within the same application context and can be ordered using the {@link ordered}
 * interface or {@link order @order} annotation.
 *
 * @author phillip webb
 * @since 1.3.0
 * @see commandlinerunner
 */
@functionalinterface
public interface applicationrunner {
 
  /**
   * callback used to run the bean.
   * @param args incoming application arguments
   * @throws exception on error
   */
  void run(applicationarguments args) throws exception;
 
}

2.2 使用

applicationrunner 和 commandlinerunner 的使用是有差別的:

  • commandlinerunner 的使用,只是把參數(shù)根據(jù)空格分割。
  • applicationrunner 會(huì)根據(jù) 是否匹配 --key=value 來解析參數(shù),
    • 能匹配,則為 optional 參數(shù), 可用getoptionvalues獲取參數(shù)值。
    • 不匹配則是 non optional 參數(shù)。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.consoleapplication;
 
import org.springframework.boot.applicationrunner;
import org.springframework.stereotype.component;
import org.springframework.boot.applicationarguments;
 
@component
public class testapplicationrunner implements applicationrunner {
 
  @override
  public void run(applicationarguments args) throws exception {
    // do something...
    system.out.println("option arg names" + args.getoptionnames());
    system.out.println("non option+" + args.getnonoptionargs());
  }
}

2.3 運(yùn)行結(jié)果

運(yùn)行命令 java -jar build/libs/consoleapplication-0.0.1-snapshot.jar -non1 non2 --option=1, 結(jié)果為:

2019-03-16 18:08:08.528  info 19778 --- [           main] c.e.consoleapplication.demoapplication   : no active profile set, falling back to default profiles: default
2019-03-16 18:08:09.166  info 19778 --- [           main] c.e.consoleapplication.demoapplication   : started demoapplication in 16.059 seconds (jvm running for 16.56)
test
option arg names[option]
non option+[-non1, non2]-non1
non2
--option=1
test%

可以看到, optional 參數(shù)名有 option, non optional 參數(shù)有 -non1 和 non2

3. 小結(jié)

commandlinerunner 和 applicationrunner 都能實(shí)現(xiàn)命令行應(yīng)用啟動(dòng)時(shí)根據(jù)參數(shù)獲取我們需要的值,做特殊的邏輯。但兩者有所不同,推薦使用 applicationrunner 的 optional 參數(shù), 方便擴(kuò)展。

4. 參考文檔

https://docs.spring.io/spring-boot/docs/2.0.5.release/reference/htmlsingle/#boot-features-web-environment

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://segmentfault.com/a/1190000018530137

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 成年黄色在线观看 | 亚洲一区二区 | 二区在线视频 | 国精产品99永久一区一区 | 国产欧美在线观看 | 人人射在线视频 | 日韩在线二区 | 国产一区二区三区视频在线观看 | 91电影院| 国产欧美日韩 | 免费观看视频毛片 | 久久久精品国产99久久精品芒果 | 久久白虎 | 黄色毛片看看 | 亚洲专区中文字幕 | 欧美成人高清视频 | 日本一区二区不卡 | 亚洲精品久 | 69中文字幕 | 午夜伦理影院 | 特级西西人体444www高清大胆 | 91精品国产综合久久久久久丝袜 | 一区二区三区无码高清视频 | 亚洲国产一区二区三区四区 | 深夜精品 | 综合色成人 | 来个一级毛片 | 欧美一区在线观看视频 | 久久综合激情 | 狠狠综合| а√天堂资源中文最新版地址 | 欧美a级免费看 | 国产精品久久久久久吹潮 | 一区二区三区精品 | 日韩成人av在线 | 精品一区二区三区免费 | 激情久久综合网 | 成人午夜精品 | 午夜视频在线 | 欧美日韩综合视频 | 国产一级一级毛片女人精品 |