EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot CommandLineRunner

Spring Boot CommandLineRunner

Updated April 1, 2023

Spring Boot CommandLineRunner

Introduction to Spring Boot CommandLineRunner

CommandLineRunner is an interface in the Spring boot framework used to load or run the piece of code after the spring boot application has started; in short command-line runners will run or execute their code after the spring boot main method has started. This is an interface which contains one method, i.e. run(); this method gets executed after the main method. We can have more than one class in our application which can implement this interface. So, in the end, it will run all the class run methods once the application context has been loaded for this.

ADVERTISEMENT
Popular Course in this category
SPRING BOOT Course Bundle - 5 Courses in 1 | 6 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of Spring Boot CommandLineRunner

It is an interface in the spring boot framework, which can be implemented by the other classes present in the application.

public class class_name
implements CommandLineRunner {
// logic goes here .. //
}

As you can see in the above example, we are implementing the CommandLineRunner interface in our class; we have to specify the class name as well.

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
// logic goes here ..//
}

How does Spring Boot CommandLineRunner Works?

As we already know, CommandLineRunner is an interface that contains only one method, which is used to run the code after the spring application has started. Inside this run() method, we can write our own logic; we can also implement this interface in more than one class in the spring boot application; there is no such restriction. If we implement the interface, we have to override the run() method and provide its implementation. Inside this method, we can write the logic which we want to execute once the application context has been loaded.

Here we will see its packages and run() method signature as well.

1. Run method signature.

Syntax:

@Override
public void run(String... args) throws Exception {
// logic goes here ..//
}

As you can see in the above method, it does not return anything, i.e. is the return type is ‘void’; also, it has taken string argument and throw Exception if it occurred in order to use this, we can to include the required package inside the application, which we will see in the second point. So inside this method, we can write the logic we want to execute once the application context is loaded. So it will run at the end of the code.

2. Also, we can create multiple classes which can implement this interface. So, in the end, all the methods of the classes will run.

3. While using this interface, we have to import the necessary packages into the application. Also, we do not require adding any extra dependency to use this interface; it is already available in the spring basic dependency only.

Below we can see the import statement for this:

Example:

import org.springframework.boot.CommandLineRunner;

This is the required package that needs to be in place; otherwise, we will get compile-time errors in the application, and it will not work.

Example of Spring Boot CommandLineRunner

Below is the working example for the command line runner in the spring boot application with all the steps mentioned that need to be taken care of while implementing this application.

Here we will see how we can use CommandLineRunner inside our application to make it work.

Let’s take a look at the step by step process for this.

a. First, we will create the spring boot project from the spring initializer, where we will mention all the necessary details it required.

b. After that, generate the zip, extract it to the machine, and import it inside the editor.

URL: https://start.spring.io/

c. After this, we can implement this interface inside the main() class of the application itself.

In order to see the working of the CommandLineRunner.

Example:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TradersApplication implements CommandLineRunner {
public static void main(String[] args)
{
SpringApplication.run(TradersApplication.class, args);
System.out.println("Application running in the dev mode !!");
}
@Override
public void run(String... args) throws Exception {
System.out.println("Here the command line runner is running inside the spring boot ,,..//");
}
}

Output:

Spring Boot CommandLineRunner 1

d. We can have more than one class which can implement this interface in the application. Also, we can write our own logic to the run() method to get it executed after the application context.

Points to Remember:

Given below are the points to remember while using it inside the application:

  • We can have many classes which can implement this interface; there is no restriction for this.
  • This interface contains only one method, which is run().
  • When we implement this interface, it becomes mandatory for us to provide the implementation for the run() method; else, it will give us a compile-time error.
  • This method takes a string argument, which we can pass after the application has started on the command prompt.

Features of Spring Boot CommandLineRunner

Given below are the features mentioned:

  • This interface provides us with the ability to load or run the code after the application context has been loaded and before the spring run method finish its execution.
  • By using it, we can easily pass the command line arguments required from our end.
  • Easy to use and handle.

Conclusion

As we have seen already that if we want to load anything or want to execute any function after the application context has been loaded for the spring application, then we can simply see this interface to execute it, simple to use and handle, and understandable by the developers as well.

Recommended Article

This is a guide to Spring Boot CommandLineRunner. Here we discuss the introduction, how spring boot CommandLineRunner works? Examples and features. You may also have a look at the following articles to learn more –

  1. Maven Repository Spring
  2. Spring Boot DevTools
  3. Spring AOP
  4. Spring Cloud Components
ADVERTISEMENT
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW