
- Spring Boot - Home
- Spring Boot - Introduction
- Spring Boot - Quick Start
- Spring Boot - Bootstrapping
- Spring Tool Suite
- Spring Boot - Tomcat Deployment
- Spring Boot - Build Systems
- Spring Boot - Code Structure
- Spring Beans & Dependency Injection
- Spring Boot - Runners
- Spring Boot - Starters
- Spring Boot - Application Properties
- Spring Boot - Configuration
- Spring Boot - Annotations
- Spring Boot - Logging
- Building RESTful Web Services
- Spring Boot - Exception Handling
- Spring Boot - Interceptor
- Spring Boot - Servlet Filter
- Spring Boot - Tomcat Port Number
- Spring Boot - Rest Template
- Spring Boot - File Handling
- Spring Boot - Service Components
- Spring Boot - Thymeleaf
- Consuming RESTful Web Services
- Spring Boot - CORS Support
- Spring Boot - Internationalization
- Spring Boot - Scheduling
- Spring Boot - Enabling HTTPS
- Spring Boot - Eureka Server
- Service Registration with Eureka
- Gateway Proxy Server and Routing
- Spring Cloud Configuration Server
- Spring Cloud Configuration Client
- Spring Boot - Actuator
- Spring Boot - Admin Server
- Spring Boot - Admin Client
- Spring Boot - Enabling Swagger2
- Spring Boot - Using SpringDoc OpenAPI
- Spring Boot - Creating Docker Image
- Tracing Micro Service Logs
- Spring Boot - Flyway Database
- Spring Boot - Sending Email
- Spring Boot - Hystrix
- Spring Boot - Web Socket
- Spring Boot - Batch Service
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - Unit Test Cases
- Rest Controller Unit Test
- Spring Boot - Database Handling
- Securing Web Applications
- Spring Boot - OAuth2 with JWT
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 Sign-In
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.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ [32m :: Spring Boot :: [39m [2m (v3.3.3)[0;39m [2m2024-09-04T12:05:41.902+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mc.tutorialspoint.demo.DemoApplication [0;39m [2m:[0;39m Starting DemoApplication using Java 21.0.3 with PID 12304 (E:\Dev\demo\target\classes started by Tutorialspoint in E:\Dev\demo) [2m2024-09-04T12:05:41.905+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mc.tutorialspoint.demo.DemoApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default" [2m2024-09-04T12:05:43.427+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port 8080 (http) [2m2024-09-04T12:05:43.454+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat] [2m2024-09-04T12:05:43.454+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.28] [2m2024-09-04T12:05:43.528+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext [2m2024-09-04T12:05:43.529+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1552 ms [2m2024-09-04T12:05:44.120+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8080 (http) with context path '/' [2m2024-09-04T12:05:44.134+05:30[0;39m [32m INFO[0;39m [35m12304[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mc.tutorialspoint.demo.DemoApplication [0;39m [2m:[0;39m Started DemoApplication in 3.007 seconds (process running for 4.519) 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.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ [32m :: Spring Boot :: [39m [2m (v3.3.3)[0;39m [2m2024-09-04T12:08:37.804+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mc.tutorialspoint.demo.DemoApplication [0;39m [2m:[0;39m Starting DemoApplication using Java 21.0.3 with PID 16908 (E:\Dev\demo\target\classes started by Tutorialspoint in E:\Dev\demo) [2m2024-09-04T12:08:37.808+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mc.tutorialspoint.demo.DemoApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default" [2m2024-09-04T12:08:38.772+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port 8080 (http) [2m2024-09-04T12:08:38.786+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat] [2m2024-09-04T12:08:38.786+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.28] [2m2024-09-04T12:08:38.841+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext [2m2024-09-04T12:08:38.842+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 980 ms [2m2024-09-04T12:08:39.185+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8080 (http) with context path '/' [2m2024-09-04T12:08:39.192+05:30[0;39m [32m INFO[0;39m [35m16908[0;39m [2m---[0;39m [2m[demo] [ main][0;39m [2m[0;39m[36mc.tutorialspoint.demo.DemoApplication [0;39m [2m:[0;39m Started DemoApplication in 1.844 seconds (process running for 2.693) Hello world from Command Line Runner