Spring-boot-runners

提供:Dev Guides
移動先:案内検索

春のブーツ-ランナー

Application Runner and Command Line Runner interfaces lets you to execute the code after the Spring Boot application is started. You can use these interfaces to perform any actions immediately after the application has started. This chapter talks about them in detail.

アプリケーションランナー

Application Runnerは、Spring Bootアプリケーションの起動後にコードを実行するために使用されるインターフェイスです。 以下の例は、メインクラスファイルにApplication Runnerインターフェースを実装する方法を示しています。

package com.finddevguides.demo;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Override
   public void run(ApplicationArguments arg0) throws Exception {
      System.out.println("Hello World from Application Runner");
   }
}

ここで、* Application Runnerの Hello World の下のコンソールウィンドウを観察すると、Tomcatの起動後にprintlnステートメントが実行されます。 次のスクリーンショットは関連していますか?

Hello World From Application Runner

コマンドラインランナー

コマンドラインランナーはインターフェースです。 Spring Bootアプリケーションの起動後にコードを実行するために使用されます。 以下の例は、メインクラスファイルにコマンドラインランナーインターフェイスを実装する方法を示しています。

package com.finddevguides.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Override
   public void run(String... arg0) throws Exception {
      System.out.println("Hello world from Command Line Runner");
   }
}

Tomcatの起動後にprintlnステートメントが実行される「コマンドラインランナーのHello world」の下のコンソールウィンドウを見てください。

コマンドラインランナー