Spring Boot Command Line Runner

What is a Command Line Runner and how can I use it in my applications?

A student asked me the other day what a Command Line Runner was and when can we use one. If you watch demos of Spring Boot features or applications you have probably seen this and said to yourself “What the heck is that?”.

Command Line Runner

If you take a look a look at the API Documentation for Command Line Runner you will get this elegant explanation. 

Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or @Order annotation.

In this article, I will explain what a Command Line Runner is and how we can take advantage of it in our applications. 

Command Line Runner

As we saw from the API documentation definition the Command Line Runner is an interface. This means that we can’t create an instance of it but we can implement it. 

If we define an implementation of the Command Line Runner interface we will need to override the run method. This method will be executed after the application context is loaded and right before the Spring Application run method is completed. 

Before we dive in and create one we should talk about some of the things you might want to use this for. Remember that this going to execute after the application context is loaded so you could use it to check if certain beans exist or what values of certain properties are. 

Another reason to use it is to load some data right before your application fires up. I will say that I have used a Command Line Runner for these purposes before but they were both in development or demo environments. 

Command Line Runner Demo

In this demo, we are going to create a DataLoader class that is going to load some initial demo data into our application. For demo purposes pretend we created a web application with JPA & H2 and a simple Image class that looks like this.

When my application starts up I want to load a couple of images into our database. I could place a SQL script in the resources folder, but I don’t care for writing SQL from scratch. 

The first thing we need to do is create a DataLoader class in our main package. This class is going to implement the Command Line Runner interface and override the run method. The other important thing to note here is that we need to annotate our class with @Component or Spring will never find it. 

Now I want to load some data. I have an Image Service that has a save method that calls a JPA repository. I will simply call that save method and pass in a few new image instances. 

If we fire up the application and look at the H2 console we will see our new database rows. 

Command Line Runner Data Loader

Advanced Command Line Runner Examples

What if you wanted to create 2 Command Line Runners that did contained completely separate logic? No problem, just use the annotation Order to specify the order in which they should run. 

If we look in the console we can see that Another Database Loader ran first.

Command Line Runner @Order Annotation

You might also see examples where the Command Line Runner is created right in the main application class. Thanks to Java 8, when we have an interface with a single method we can implement it using a lambda expression. This helps us cut down some of the boilerplate code that creating a class to implement an interface that contains a single method creates. 

Conclusion

I hope this helped clear up some confusion on what a Command Line Runner is and how to use it. If you’re already using a Command Line Runner in your applications I would like to leave you with this question to kick start a discussion. 

Question: What are some other uses for a Command Line Runner?

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 17+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 22,000 students.

When I am not writing software I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.