EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot CommandLineRunner
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot CommandLineRunner

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.

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 .. //
}

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

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
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

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

EDUCBA Login

Forgot Password?

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

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

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

EDUCBA

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

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

Let’s Get Started

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