Spring Boot Resources

Spring Boot - Runners



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

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

DemoApplication.java

package com.tutorialspoint.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");
   }
}

Console Output

Now, if you observe the console window output below Hello World from Application Runner, the println statement is executed after the Tomcat started.


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.6)

2025-09-28T14:24:27.094+05:30  INFO 43596 --- [demo] [           main] c.tutorialspoint.demo.DemoApplication    : Starting DemoApplication using Java 21.0.6 with PID 43596 (D:\Projects\demo\target\classes started by mahes in D:\Projects\demo)
2025-09-28T14:24:27.097+05:30  INFO 43596 --- [demo] [           main] c.tutorialspoint.demo.DemoApplication    : No active profile set, falling back to 1 default profile: "default"
2025-09-28T14:24:28.225+05:30  INFO 43596 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-09-28T14:24:28.242+05:30  INFO 43596 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-09-28T14:24:28.242+05:30  INFO 43596 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.46]
2025-09-28T14:24:28.309+05:30  INFO 43596 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-09-28T14:24:28.310+05:30  INFO 43596 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1147 ms
2025-09-28T14:24:28.728+05:30  INFO 43596 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-09-28T14:24:28.740+05:30  INFO 43596 --- [demo] [           main] c.tutorialspoint.demo.DemoApplication    : Started DemoApplication in 2.163 seconds (process running for 2.487)
Hello World from Application Runner

Command Line Runner

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

DemoApplication.java

package com.tutorialspoint.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");
   }
}

Console Output

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


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.6)

2025-09-28T14:25:20.792+05:30  INFO 41164 --- [demo] [           main] c.tutorialspoint.demo.DemoApplication    : Starting DemoApplication using Java 21.0.6 with PID 41164 (D:\Projects\demo\target\classes started by mahes in D:\Projects\demo)
2025-09-28T14:25:20.797+05:30  INFO 41164 --- [demo] [           main] c.tutorialspoint.demo.DemoApplication    : No active profile set, falling back to 1 default profile: "default"
2025-09-28T14:25:21.880+05:30  INFO 41164 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-09-28T14:25:21.899+05:30  INFO 41164 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-09-28T14:25:21.900+05:30  INFO 41164 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.46]
2025-09-28T14:25:21.961+05:30  INFO 41164 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-09-28T14:25:21.962+05:30  INFO 41164 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1099 ms
2025-09-28T14:25:22.416+05:30  INFO 41164 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-09-28T14:25:22.425+05:30  INFO 41164 --- [demo] [           main] c.tutorialspoint.demo.DemoApplication    : Started DemoApplication in 2.163 seconds (process running for 2.558)
Hello world from Command Line Runner
Advertisements