[Spring Bootシリーズ教程]Spring BootApple plication
Spring Boot
から提供されたDevTools
を紹介しました.本記事では、SpringApplication
の種類を紹介します.前の記事では、
SpringApplication
がSpring Boot
アプリケーションの入口であることを知り、本明細書では、SpringApplication
の他のいくつかの特性を詳細に紹介する.アプリケーションを実行
SpringApplication
は、run
方法によってmain
アプリケーションを起動することができる静的方法を提供する.以下の通りですpublic static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}
アプリケーションの起動が成功したら、以下の内容を見るべきです.. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: v2.1.0.RELEASE
2013-07-31 00:08:16.117 INFO 56603 --- [ main] o.s.b.s.app.SampleApplication : Starting SampleApplication v0.1.0 on mycomputer with PID 56603 (/apps/myapp.jar started by pwebb)
2013-07-31 00:08:16.166 INFO 56603 --- [ main] ationConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.ser[emailprotected]6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; root of context hierarchy
2014-03-04 13:09:54.912 INFO 41370 --- [ main] .t.TomcatServletWebServerFactory : Server initialized with port: 8080
2014-03-04 13:09:56.501 INFO 41370 --- [ main] o.s.b.s.app.SampleApplication : Started SampleApplication in 2.992 seconds (JVM running for 3.658)
デフォルトでは、Spring
レベルのログが印刷され、アプリケーションポート、プログラムINFO
、プログラムを起動するユーザなどの起動関連メッセージが出力される.より詳細な情報を参照したい場合は、外部構成により、PID
モードでアプリケーションを起動するか、またはシステムログの構成を直接修正することにより、より詳細な情報を表示するように指定することができる.起動失敗アナライザ
あなたのアプリケーションが起動に失敗した時、以下のようなエラーメッセージがあります.
***************************
APPLICATION FAILED TO START
***************************
Description:
Embedded servlet container failed to start. Port 8080 was already in use.
Action:
Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
以上のヒントはdebug
から提供されたSpring Boot
によって示されており、なぜアプリケーションが起動できないのかをより明確に知ることができます.FailureAnalyzers
はすでに一般的ないくつかの8080
を内蔵しています.これらがあなたのプロジェクトの需要を満たすことができないなら、あなたも自分でそれを実現することができます.もちろん、起動異常を処理できるものがなければ、他の方法で詳細な異常スタック情報を取得することもできる.
SPring Boot
モードを使用してアプリケーションを起動するFailureAnalyzers
レベル例えば、
FailureAnalyzers
で起動されたアプリケーションを使用するなら、debug
モードを以下のように有効にすることができる.$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug
カスタムBannerこれは非常に面白い機能です.
debug
とは、システムが起動した時、最初に印刷した内容を指します.. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: v1.5.9.RELEASE
以上の内容は自分で定義できます.私たちはプロジェクトjava -jar
の下にdebug
を追加し、中にBanner
の内容を入力すればよく、デフォルトではclasspath
を使用して解析を符号化します.もしコードを制定する必要があれば、banner.txt
の属性を設定することができます.下の表にbanner
でサポートされている変数が一覧されています.Varable
Description
banner.txt
The version number of your appration as declared in UTF-8
.For example banner.charset
is printesd as banner.txt
.${application.version}
The version number of your appration as declead in MANIFEST.MF
formated for display(surrounded with bracts and prefixed with Implementation-Version: 1.0
)For exple 1.0
.${application.formatted-version}
The Spring Boot version that you are using.For example MANIFEST.MF
.v
The Spring Boot version that you are using formatited for display(surround ed with braackets and prefixed with (v1.0)
).For example ${spring-boot.version}
.1.5.19.RELEASE
(or ${spring-boot.formatted-version}
、v
、(v1.5.19.RELEASE)
)Where
${Ansi.NAME}
is the name of an ANSI escape code.See ${AnsiColor.NAME}
for details.${AnsiBackground.NAME}
The title of your appration as declead in ${AnsiStyle.NAME}
.For example NAME
is printesd as AnsiPropertySource
.最も強力なのは、
${application.title}
は文字だけではなく、MANIFEST.MF
およびImplementation-Title: MyApp
フォーマットの画像をサポートすることができ、MyApp
属性を設定すればよく、興味のあるものは自分で実験に行くことができます.banner
を無効にしたいなら、とても簡単です.spring:
main:
banner-mode: "off"
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
カスタムSpringApplicationデフォルトの設定があなたの要求を満たしていないなら、あなたも自分で定義できます.カスタム
.git
には、2つの方法があります.第1は従来の方法であり、上記のようにクラスの属性値を設定します.もう一つの方法は、.jpg
モードを使用して設定することである.new SpringApplicationBuilder()
.sources(Parent.class)
.child(Application.class)
.bannerMode(Banner.Mode.OFF)
.run(args);
アプリケーションイベントとその傍受banner.image.location
は起動、運転、クローズの時に、一連のイベントを登録して購読します.ここでは、いくつかのイベントは
banner
が作成される前にトリガされるので、それらを@Beanとして登録することはできません.SpringApplication
またはbanner mode
の方式でしか登録できません.自動登録を望むなら、アプリケーションBuilder
の下にSpringApplication
ファイルを追加して、以下の内容を追加しても良いです.ApplicationContext
以下はあなたが実行を適用する時、いくつかのイベントの発生順序です.SpringApplication.addListeners(…)
SpringApplicationBuilder.listeners(…)
classpath
META-INF/spring.factories
org.springframework.context.ApplicationListener=com.example.project.MyListener
ApplicationStartingEvent
アプリケーションは、正しいアプリケーションコンテキストの作成を試みることができる.デフォルトの場合、ApplicationEnvironmentPreparedEvent
またはApplicationPreparedEvent
は作成されます.具体的にどのコンテキストを作成するかは、あなたが開発したのがApplicationReadyEvent
プログラムかどうかに基づいています.もちろん、プログラミングによって指定してもいいです.ApplicationFailedEvent
.アプリケーションのパラメータを取得
コマンドラインまたは他の方法でアプリケーションのパラメータを指定する必要がある場合、これらのパラメータは以下のように取得できます.
*Application Agments*を通じて
Spring Boot
インターフェースは、非常に便利なアクセスアプリケーションのパラメータを提供する.public interface ApplicationArguments {
String[] getSourceArgs();
Set<String> getOptionNames();
boolean containsOption(String name);
List<String> getOptionValues(String name);
List<String> getNonOptionArgs();
}
例えば:import org.springframework.boot.*
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*
@Component
public class MyBean {
@Autowired
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
}
}
@Valueで注解するAnnotationConfigApplicationContext
は、AnnotationConfigEmbeddedWebApplicationContext
にアプリケーションパラメータを登録し、これにより、Web
の注釈方式でこれらのパラメータにアクセスすることができる.Application RunnerまたはCommandLinerを使用する
プログラム起動時に特定の特殊コードを実行する必要がある場合、
setWebEnvironment(boolean webEnvironment)
またはApplicationArguments
を実装してもいいです.これらはSpring Boot
の方法を提供しています.この方法はCommandLinePropertySource
の実行が完了する前に呼び出されます.両者は入館以外は同じです.@Value
は文字列配列をパラメータとして使用し、ApplicationRunner
はCommandLineRunner
をパラメータとして使用する.例えば:import org.springframework.boot.*
import org.springframework.stereotype.*
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
アプリケーションの終了各
run
は、アプリケーションが優雅に閉じられていることを保証するために、クローズフックを登録する.Admin特性
省略する
おわりに
本論文では、
SpringApplication.run(…)
のこのクラスのいくつかの特性を紹介し、次の記事では、CommandLineRunner
のアプリケーションの外部構成を紹介する.