Spring Boot, a popular Java-based framework, offers many features to simplify application development and deployment. One of these features is the ability to run code automatically when the application starts. This can be particularly useful for initializing databases, setting up application properties, or running background tasks. In this article, we'll explore how to run code on startup in a Spring Boot application.
![Top 7 Spring Boot Online Courses for Beginners in 2025 - Best of Lot [UPDATED]](https://i.pinimg.com/originals/dd/e7/b2/dde7b2eb36c4e18daf0e04025b9f77b3.png)
Before we dive in, it's important to note that there are several ways to achieve this in Spring Boot. We'll cover two common methods: using ApplicationRunner and CommandLineRunner interfaces, and configuring a customizable ListenableFuture.

Using ApplicationRunner and CommandLineRunner Interfaces
Spring Boot provides two interfaces, ApplicationRunner and CommandLineRunner, which allow you to specify a method to run when the application starts. Both interfaces have a single method with the following signature:

void run(String... args) throws Exception;
While they both serve the same purpose, there's a slight difference in how they handle input arguments. Let's explore each interface:
ApplicationRunner

ApplicationRunner allows you to inject the application arguments directly into the run method. This can be useful if you need to process command-line arguments during startup. Here's an example:
```java @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(String... args) throws Exception { for (String arg : args) { System.out.println("Received argument: " + arg); } } } ```
In this example, the run method will print out each command-line argument received when the application starts.
CommandLineRunner

CommandLineRunner, on the other hand, doesn't expose the command-line arguments directly. Instead, it focuses on running an application-specific task during startup. Here's an example:
```java @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Application is starting..."); // Add your startup logic here } } ```
In this case, the run method contains the logic that Spring Boot will execute automatically when the application starts.
Configuring a Customizable ListenableFuture

Another approach to running code on startup is to configure a ListenableFuture that can be customized and managed more finely. A ListenableFuture can offer more control over the startup sequence, as it allows you to specify dependencies between tasks. Here's an example using Spring's ListenableFutureCallback:
Setting up a ListenableFuture









First, create a method that returns a ListenableFuture. This method will be called when the application starts:
```java @SpringBootApplication public class MyApplication implements CommandLineRunner { @Override public void run(String... args) throws Exception { initDatabase(); } private ListenableFuture initDatabase() { // Initialization logic here return new AsyncResult<>("Database initialized"); } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ```
In this example, the initDatabase method returns a ListenableFuture that will be executed automatically when the application starts.
Adding a ListenableFutureCallback
Next, add a ListenableFutureCallback to listen for the result of the ListenableFuture and execute additional logic once the task completes:
```java @SpringBootApplication public class MyApplication implements CommandLineRunner { // ... (initDatabase method as before) @Async @Bean public ListenableFutureCallback listener() { return new ListenableFutureCallback<>("Database initialization completed"); } // ... (main method as before) } ```
In this setup, the listener method will be called once the initDatabase task completes, allowing you to add more startup tasks that depend on the database initialization.
By using these approaches, you can easily run code on startup in a Spring Boot application, simplifying the process of initializing your application's resources and services. Happy coding!