Spring Boot CommandLineRunner Interface Example
Spring Boot’s CommandLineRunner
interface, allows developers to execute code when the Spring Boot application starts. In this blog, we’ll explore the CommandLineRunner
interface, and its purpose, and provide examples of its usage.
Understanding the CommandLineRunner Interface
The CommandLineRunner
interface is part of the Spring Boot framework. It belongs to the org.springframework.boot
package and is designed to execute code at the startup of a Spring Boot application. It is a functional interface, which means it has a single abstract method, void run(String... args)
. This method is invoked when the Spring application context is initialized.
The run
method receives a variable number of command-line arguments as an array of strings. This allows you to pass arguments to your application at startup, which can be useful for configuration and customization.
Let’s explore some common usages of the CommandLineRunner
interface.
1. Basic Usage
In this example, we’ll create a simple Spring Boot application that prints a message at startup. First, create a Spring Boot application with the Spring Initializer or your favorite IDE.
package org.websparrow;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootApp implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("Hello from CommandLineRunner!");
}
}
When you run the application, it will print “Hello from CommandLineRunner!” during startup.
Similar Posts:
2. Using Command-Line Arguments
The CommandLineRunner
interface allows you to access command-line arguments passed to your application. Let’s modify our example to use these arguments:
package org.websparrow;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootApp implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, "arg1", "arg2");
}
@Override
public void run(String... args) throws Exception {
System.out.println("Command-Line Arguments:");
for (String arg : args) {
System.out.println(arg);
}
}
}
Now, when you run the application, it will print the command-line arguments passed to it.
3. Executing Custom Logic
You can use the CommandLineRunner
to execute custom logic or perform tasks at application startup. For instance, you can load configuration settings, initialize database connections, or perform any other setup needed for your application.
package org.websparrow;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootApp implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
}
@Override
public void run(String... args) throws Exception {
// Custom initialization logic
System.out.println("Executing custom initialization logic...");
}
}
This demonstrates how you can execute custom logic during application startup.
Summary
The CommandLineRunner
interface is a valuable tool in Spring Boot for executing code when your application starts. Whether you want to print messages, process command-line arguments, or perform complex initialization tasks, this interface can be a powerful addition to your Spring Boot applications. In summary, the CommandLineRunner
interface simplifies the process of running code at application startup, making Spring Boot even more flexible and powerful for developers.
References
Similar Posts
- Spring Web MVC (Model View Controller) Introduction and Features
- @ConfigurationProperties Annotation in Spring Boot
- How to send email using Spring Boot
- How to get user details in Spring Security
- @ControllerAdvice vs. @RestControllerAdvice in Spring
- Versioning of REST API in Spring Boot: Types and Benefits
- Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
- Spring AOP + AspectJ @Pointcut Annotation Example
- How to set custom favicon icon in Spring Boot
- Spring Properties Dependency Injection Example