Spring Boot Command Line Runner Example

While developing spring applications, we may have a situation, where we want to execute a piece of code only once on application startup. For example, in our last project, we had a requirement to reload a particular Redis cache value from database data on application startup, only if the cache value is empty on the Redis cache server.

For this kind of requirement, spring provides an interface called CommandLineRunner, which contains only a run() method. This run() method executes only once during spring application startup.

We can use the command line runner in multiple ways. We can implement the CommandLineRunner interface on a new java class and by registering bean of that class on the spring container.

Also, we can implement the CommandLineRunner interface directly on the spring boot application class.

In this article, we will learn about the command line runner interface and how to use it on our spring boot applications.

Implementing Command Line Runner in spring boot application

Implementing the command line runner in the spring boot application is very simple. We have to implement the CommandLineRunner interface, override the run() method by providing the required implementation.

Create a spring boot application with required starter dependencies.

Create a java class with the name StartupRunner. This is our startup runner class, which implements CommandLineRunner. This class’s run() method will contain a piece of code, which the spring boot on application executes during startup.

package com.asb.example;

import org.springframework.boot.CommandLineRunner;

public class StartupRunner implements CommandLineRunner {

	@Override
	public void run(String... args) throws Exception {
		System.out.println("Command Line Runner class invoked!!");
	}
}

Define a bean configuration for the created StartupRunner class inside the spring boot application as given below.

package com.asb.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootCommandLineRunnerExampleApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootCommandLineRunnerExampleApplication.class, args);
	}

	@Bean
	public StartupRunner StartupRunner() {
		return new StartupRunner();
	}
}

Alternative Ways

We can also make use of the @Component annotation. This annotation registers the command line runner implementation class as bean during the component scan.

The below is the code snippet of the StartupRunner class with the @Component annotation.

@Component
public class StartupRunner implements CommandLineRunner {
        @Override
	public void run(String... args) throws Exception {
		System.out.println("Command Line Runner class invoked!!");
	}
}

We can also implement CommandLineRunner directly on our spring boot application class as shown below. 🙂

@SpringBootApplication
public class SpringBootCommandLineRunnerExampleApplication implements CommandLineRunner {

	//Spring boot 

	@Override
	public void run(String... args) throws Exception {
		System.out.println("Command Line Runner class invoked!!");
      }
}

Testing the output

It’s all set. Now start the spring boot application.

Once the spring boot application starts completely, the command line runner implementation is invoked.

The output of the command line runner example is shown below.

Notice the highlighted console output, which is written to console by the command line runner implementation class.

command line runner spring boot

Few important points about Command Line Runner

  • Spring batch depends on the CommandLineRunner interface to launch the execution of jobs. Spring batch’s JobLauncherCommandLineRunner implements the CommandLineRunner interface.
  • The spring boot application executes the CommandLineRunner implementation only once after the application starts. By this time, spring container is completely ready with all bean configurations, so we can use these beans for dependency injection. For Example: beans like data source, etc can be readily available inside the run() method of our command line runner implementation class.
  • Make sure to use try catch blocks to avoid complete application shut down in case of any error/exception, which may occur during command line runner execution.

Conclusion

Command Line Runner is a very useful feature of spring, which we can use to execute any piece of code, just after application startup.

We should be careful about the exceptions/errors that may occur in our implementation and handle them properly to avoid application shutdown during the command line runner implementation execution.

The sample code is available here.