[Spring Bootシリーズ教程]Spring BootApple plication


  • [Spring Bootシリーズ教程]目次
  • ソースコード(demo 03)
  • 前の記事では、Spring Bootから提供されたDevToolsを紹介しました.本記事では、SpringApplicationの種類を紹介します.
    前の記事では、SpringApplicationSpring 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
    Descriptionbanner.txtThe 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.vThe 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
  • Web環境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は文字列配列をパラメータとして使用し、ApplicationRunnerCommandLineRunnerをパラメータとして使用する.例えば:
    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のアプリケーションの外部構成を紹介する.