#
CommandLineRunner in Spring Boot
This tutorial explains how we can use CommandLineRunner interface in Java Spring Boot.
CommandLineRunner
is a Functional Interface which contains only one method "run()".
After the Spring boot application has started, Spring Boot will execute the code which implements this class (the run()
methods). The order of execution for these implementations is defined by @Order
value.
The classes which implement the CommandLineRunner interface must be a Bean.
Please take a look at the following example and read carefully the comments. The code is self-explanatory.
This example is created from a simple Spring Boot application created with Spring Initializr. I am using Maven, Java 17, Spring Boot 3.1.0.
From the base application downloaded from Spring Initializr, I updated the main class and I added some new classes as below:
package com.example.demo;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public void myMethod(String str1){
System.out.println(">>>>> Hello from myService/ myMethod() ... "+str1);
}
}
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value=1)
public class CommandLineRunnerImpl1 implements CommandLineRunner {
@Autowired
private ApplicationContext appContext;
@Override
public void run(String... args) throws Exception {
System.out.println("Order1 >>> CommandLineRunnerImpl1 - Start");
Thread.sleep(4000);
// You can see that the Application Context is created at this point
MyService myService = appContext.getBean(MyService.class);
myService.myMethod("from CommandLineRunnerImpl1");
System.out.println("Order1 >>> CommandLineRunnerImpl1 - End after 4 sec");
}
}
package com.example.demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value=2)
public class CommandLineRunnerImpl2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Order2 >>> CommandLineRunnerImpl2 - Start");
Thread.sleep(2000);
System.out.println("Order2 >>> CommandLineRunnerImpl2 - End after 2 sec");
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws InterruptedException {
System.out.println("DemoApplication - start1");
// Create the application context and start Spring Boot
ApplicationContext appContext = SpringApplication.run(DemoApplication.class, args);
System.out.println("DemoApplication - start2");
// Get a Bean fron the application context and run a method
MyService myService = appContext.getBean(MyService.class);
myService.myMethod("from main");
System.out.println("DemoApplication - end");
}
}
When I run this code I get the following log:
DemoApplication - start1
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.0)
2023-06-10T16:19:43.111+03:00 INFO 25444 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 17.0.2 with PID 25444 (D:\examples\demo\target\classes started by Catalin in D:\examples\demo)
2023-06-10T16:19:43.115+03:00 INFO 25444 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2023-06-10T16:19:43.597+03:00 INFO 25444 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 0.826 seconds (process running for 1.109)
Order1 >>> CommandLineRunnerImpl1 - Start
>>>>> Hello from myService/ myMethod() ... from CommandLineRunnerImpl1
Order1 >>> CommandLineRunnerImpl1 - End after 4 sec
Order2 >>> CommandLineRunnerImpl2 - Start
Order2 >>> CommandLineRunnerImpl2 - End after 2 sec
DemoApplication - start2
>>>>> Hello from myService/ myMethod() ... from main
DemoApplication - end
Process finished with exit code 0
Conclusion
ApplicationContext appContext = SpringApplication.run(DemoApplication.class, args);
command creates the application context. Immediately, CommandLineRunner interface implementations will be executed.- We can have many CommandLineRunner interface implementations
- CommandLineRunner interface implementations will run before the code we have in the
main
class.