English 中文(简体)
Spring Boot Tutorial

Spring Boot Resources

Selected Reading

Spring Boot - Runners
  • 时间:2024-12-22

Spring Boot - Runners


Previous Page Next Page  

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

Apppcation Runner

Apppcation Runner is an interface used to execute the code after the Spring Boot apppcation started. The example given below shows how to implement the Apppcation Runner interface on the main class file.

package com.tutorialspoint.demo;

import org.springframework.boot.ApppcationArguments;
import org.springframework.boot.ApppcationRunner;
import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;

@SpringBootApppcation
pubpc class DemoApppcation implements ApppcationRunner {
   pubpc static void main(String[] args) {
      SpringApppcation.run(DemoApppcation.class, args);
   }
   @Override
   pubpc void run(ApppcationArguments arg0) throws Exception {
      System.out.println("Hello World from Apppcation Runner");
   }
}

Now, if you observe the console window below Hello World from Apppcation Runner, the println statement is executed after the Tomcat started. Is the following screenshot relevant?

Hello World From Apppcation Runner

Command Line Runner

Command Line Runner is an interface. It is used to execute the code after the Spring Boot apppcation started. The example given below shows how to implement the Command Line Runner interface on the main class file.

package com.tutorialspoint.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;

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

Look at the console window below “Hello world from Command Line Runner” println statement is executed after the Tomcat started.

Command Line Runner Advertisements