spring boot學習(4): 命令行啓動

在使用spring boot 構建應用啓動時,咱們在工做中都是經過命令行來啓動應用,有時候會須要一些特定的參數以在應用啓動時,作一些初始化的操做。html

spring boot 提供了 CommandLineRunnerApplicationRunner 這兩個接口供用戶使用。java

1. CommandLineRunner

1.1 聲明:

@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 使用:

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 運行結果

運行: java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -sdfsaf sdfas, 結果以下:web

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 聲明

/**
 * 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 使用

ApplicationRunnerCommandLineRunner 的使用是有差異的:spring

  • CommandLineRunner 的使用,只是把參數根據空格分割。
  • ApplicationRunner 會根據 是否匹配 --key=value 來解析參數,
    • 能匹配,則爲 optional 參數, 可用getOptionValues獲取參數值。
    • 不匹配則是 non optional 參數。
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 運行結果

運行命令 java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -non1 non2 --option=1, 結果爲:bash

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 參數名有 option, non optional 參數有 -non1non2app

3. 小結

CommandLineRunnerApplicationRunner 都能實現命令行應用啓動時根據參數獲取咱們須要的值,作特殊的邏輯。但二者有所不一樣,推薦使用 ApplicationRunneroptional 參數, 方便擴展。ide

4. 參考文檔

docs.spring.io/spring-boot…spring-boot

相關文章
相關標籤/搜索