Spring Boot CommandLineRunner

By Arvind Rai, June 11, 2024
Spring CommandLineRunner is a functional interface. The beans created by implementing this interface, run before application startup. It is useful to process some data before application startup.
We can create multiple beans using CommandLineRunner and their order can be decided using Ordered interface or @Order annotation.
CommandLineRunner interface has following method to be implemented.
void run(String... args) throws Exception 
args is incoming main method arguments.
It returns void and on error it throws Exception.

run() method uses the same arguments as of main method, hence while starting application using command prompt, we can pass arguments to run() method from command prompt.

Purposes of CommandLineRunner

As we know that beans created using CommandLineRunner interface, run before application startup, we can use it for following purposes.
1. Perform some database operations before application startup.
2. Running some health check-up scripts before application startup.
3. Perform some operations for the arguments passed by command line before application startup.
4. Start any scheduler before application startup.
5. Log any message before application startup.

Creating CommandLineRunner

1. Using @Component:
Create a component implementing CommandLineRunner interface.
@SpringBootApplication
public class MyApplication {
	private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);

	public static void main(String[] args) {
		SpringApplicationBuilder application = new SpringApplicationBuilder(MyApplication.class);
		ConfigurableApplicationContext context = application.run(args);
		MessageService service = context.getBean(MessageService.class);
		logger.info(service.getMessage());

	}
}

@Component
class MyCmdLineRunner implements CommandLineRunner {
	private static final Logger logger = LoggerFactory.getLogger(MyCmdLineRunner.class);

	public void run(String... args) {
		logger.info("Executing CommandLineRunner ... ");
	}
}

@Service
class MessageService {
	public String getMessage() {
		return "Hello World!";
	}
} 
In the above code, I have created a component MyCmdLineRunner by implementing CommandLineRunner. I have defined its run() method.
When we run the application, it will run before serving any request. To test it, I have created a service and I am executing it using Spring boot application. In the output, we will see that MyCmdLineRunner will output first.
com.tadstack.MyCmdLineRunner         : Executing CommandLineRunner ... 
com.tadstack.MyApplication           : Hello World! 

2. Implementing CommandLineRunner by Main Class :
Our Spring Boot Main class can directly implement CommandLineRunner interface and override run() method. Find the example.
@SpringBootApplication
public class MyApplication implements CommandLineRunner {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(MyApplication.class);
		application.run(args);
	}

	@Override
	public void run(String... args) {
		System.out.println("Executing CommandLineRunner...");
	}
} 

Passing Arguments from Command Prompt

We can pass arguments to CommandLineRunner using command prompt.
mvn spring-boot:run -Dspring-boot.run.arguments="xyz" 
Command line arguments are accepted by Main method, and hence passed to run() method.
@Override
public void run(String... args) {
	for(String s: args) {
		System.out.println("args:" + s);
	}
} 
Output
com.tadstack.MyApplication           : Started MyApplication in 0.256 seconds (process running for 0.369)
args:xyz 

Ordering of Multiple CommandLineRunner

We can create more than one CommandLineRunner beans. To set their ordering of execution, we can use Ordered interface or @Order annotation. Find the example.
CmdLineRunner1.java
@Order(2)
@Component
public class CmdLineRunner1 implements CommandLineRunner {
	private static final Logger logger = LoggerFactory.getLogger(CmdLineRunner1.class);

	@Override
	public void run(String... args) {
		logger.info("CmdLineRunner1...");
	}
}
CmdLineRunner2.java
@Order(1)
@Component
public class CmdLineRunner2 implements CommandLineRunner {
	private static final Logger logger = LoggerFactory.getLogger(CmdLineRunner2.class);

	@Override
	public void run(String... args) {
		logger.info("CmdLineRunner2...");
	}
} 
MyApplication.java
@SpringBootApplication
public class MyApplication {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(MyApplication.class);
		application.run(args);
	}
}
Find the output.
com.tadstack.CmdLineRunner2          : CmdLineRunner2...
com.tadstack.CmdLineRunner1          : CmdLineRunner1... 

Reference





©2024 tadstack.com | Privacy Policy | Contact Us