spring bootコマンドライン起動の方式
spring bootを使ってアプリケーションを構築して起動する時、私達は仕事の中でコマンドラインでアプリケーションを起動します。アプリケーションが起動する時、いくつかの初期化の操作をするために、特定のパラメータが必要になる場合があります。
spring bootはCommundLinerとAppleication Runnerの二つのインターフェースをユーザーに提供しています。
1.Command Lineline Runner
1.1宣言:
実行:
2019-03-16 17:31:56.544 INFO 18679--[ main]c.e.co.oleapplications.DemoApple plication : No active profile set,falling back to default profiles:default
2019-03-16 17:31:57.195 INFO 18679--[ main]c.e.co.oleapplications.DemoApple plication : Stared DemoApple ation in 16.172 seconds(JVM running for 16.65)
-sdfsaf
sdfas
test command runner%
2.Application Runner
2.1声明
Application RunnerとCommundLineline Runnerの使用には違いがあります。 CommundLinerの使用は、パラメータをスペースによって分割するだけです。 Application Runnerは、適合するかどうか--key=valueによってパラメータを解析し、 が一致すると、オプティファクトパラメータとなり、get OptionValuesでパラメータ値を取得できます。 不一致はnon optionalパラメータです。
コマンド
2019-03-16 18:08:08.28 INFO 19778-[ main]c.e.co.oleapplications.DemoApple plication : No active profile set,falling back to default profiles:default
2019-03-16 18:08:091.166 INFO 19778-[ main]c.e.co.oleapplications.DemoApple plication : Stared DemoApple ation in 16.059 seconds(JVM running for 16.56)
test
option arg names[option]
non option+[-non 1,non 2]-non 1
non 2
--option=1
test
optionalパラメータ名はoption、non optionlパラメータは-non 1とnon 2があります。
3.結び目
CommundLineline RunnerとApplication Runnerはいずれもコマンドラインアプリケーションの起動時にパラメータによって必要な値を取得し、特殊な論理を行うことができます。しかし、両者は違っています。Application Runnerのoptionalパラメータを使って、拡張しやすいことをオススメします。
4.参考文書
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-feat ures-web-environment
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。
spring bootはCommundLinerとAppleication Runnerの二つのインターフェースをユーザーに提供しています。
1.Command Lineline Runner
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,
結果は以下の通りです2019-03-16 17:31:56.544 INFO 18679--[ main]c.e.co.oleapplications.DemoApple plication : No active profile set,falling back to default profiles:default
2019-03-16 17:31:57.195 INFO 18679--[ main]c.e.co.oleapplications.DemoApple plication : Stared DemoApple ation in 16.172 seconds(JVM running for 16.65)
-sdfsaf
sdfas
test command runner%
2.Application Runner
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使用Application RunnerとCommundLineline Runnerの使用には違いがあります。
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
を実行します。2019-03-16 18:08:08.28 INFO 19778-[ main]c.e.co.oleapplications.DemoApple plication : No active profile set,falling back to default profiles:default
2019-03-16 18:08:091.166 INFO 19778-[ main]c.e.co.oleapplications.DemoApple plication : Stared DemoApple ation in 16.059 seconds(JVM running for 16.56)
test
option arg names[option]
non option+[-non 1,non 2]-non 1
non 2
--option=1
test
optionalパラメータ名はoption、non optionlパラメータは-non 1とnon 2があります。
3.結び目
CommundLineline RunnerとApplication Runnerはいずれもコマンドラインアプリケーションの起動時にパラメータによって必要な値を取得し、特殊な論理を行うことができます。しかし、両者は違っています。Application Runnerのoptionalパラメータを使って、拡張しやすいことをオススメします。
4.参考文書
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-feat ures-web-environment
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。