While developing spring applications, we may 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 particular Redis cache value from database data on application startup, only if cache value is empty on Redis cache server.

For this kind of requirements, 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 command line runner in multiple ways. We can implement CommandLineRunner interface on a new java class and by registering bean of that class on spring container.

We can also implement CommandLineRunner interface directly on spring boot application class.

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

Implementing Command Line Runner in spring boot application

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

Create a spring boot application with required starter dependencies.

Create a java class called StartupRunner.java. This is our startup runner class, which implements CommandLineRunner. This class’s run() method will contain piece of code, which we want to be executed on application startup.

startuprunner.java
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 spring boot application as given below.

SpringBootCommandLineRunnerExampleApplication.java
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 @Component annotation, which registers our command line runner implementation class as bean during component scan.

Code snippet of our StartupRunner class with @Component annotation is given below.

@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!!");
      }
}

It’s all set. Now start the spring boot application. ๐Ÿ™‚

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

Output of our command line runner example is shown below. Notice the highlighted console output, which is written to console by our command line runner implementation class.

Output
command line runner spring boot

Few important points about Command Line Runner

  • Spring batch depends on CommandLineRunner interface to launch the execution of jobs. Spring batch’s JobLauncherCommandLineRunner implements CommandLineRunner interface.
  • CommandLineRunner implementation is executed only once after the application is started. By this time, spring container is completely ready with all configured beans resisted in spring Ioc container, so we can use these beans for dependency injection. for Example: beans like data source, etc can be readily available inside 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 very useful feature of spring, which can be used to execute any piece of code, just after application startup. We should be careful about the exception/errors that may occur in our implementation and handle them properly to avoid application shutdown during command line runner implementation execution.

Sample code is available here. Happy coding ๐Ÿ™‚ ๐Ÿ™‚

You may also interested in