Implementation to Run a CommandLineRunner Bean Conditionally in Spring Boot Application

Below are the implementation steps to run a CommandLineRunner Bean Conditionally in Spring Boot.

Step 1: Project Creation

Create a new Spring Boot project using Spring Initializr and include specific dependencies

  • Spring Web
  • Spring DevTools
  • Lombok

Once we create the project, the file structure typically looks like this:


Step 2: Define Properties

Open the application.properties file and enable the app runner of the Spring Boot application.

spring.application.name=spring-commandlineRunner-demo
app.runner.enabled=true


Step 3: Define the Conditional Bean into the main class.

Go to src > org.example.springcommandlinerunnerdemo > SpringCommandlineRunnerDemoApplication and put the below code.

Java
package org.example.springcommandlinerunnerdemo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringCommandlineRunnerDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCommandlineRunnerDemoApplication.class, args);
    }

    @Bean
    @ConditionalOnProperty(name = "app.runner.enabled", havingValue = "true")
    public CommandLineRunner conditionalRunner() {
        return args -> {
            System.out.println("Conditional CommandLineRunner running!");
        };
    }
}


Step 4: Run the application

Now, we will run the application, and it will start on port 8080. If the app.runner.enable property is set to true, then the conditional bean of the Spring Boot application will be executed.


This example demonstrates how to use Spring Boot’s @ConditionalOnProperty and profiles to control the execution of the CommandLineRunner beans.


How to Run a CommandLineRunner Bean Conditionally in Spring Boot?

In Spring Boot, CommandLineRunner is an interface used to execute code once the application startup is complete. This feature is especially useful for running initialization logic or checks right after the application starts. However, there may be situations where you want to run the CommandLineRunner implementation conditionally based on certain factors such as the presence of a system property, configuration setting, or environment.

Conditional execution in Spring Boot can be achieved using the @Conditional family of annotations provided by Spring. These annotations allow you to specify conditions under which the bean is included in the application context. For running CommandLineRunner beans conditionally.

Key Terminologies:

  • @ConditionalProperty: Executes the bean based on the presence and value of a specific configuration property.
  • @Profile: Restricts the bean to only when a specific profile is active.
  • CommandLineRunner: Interface in Spring Boot used for executing code after the application context is loaded. Implementing the interface allows you to run specific pieces of code when the application starts.
  • Bean: In the Spring framework, a bean is an object that can be instantiated, assembled, and otherwise managed by the Spring IoC (Inversion of Control) container. Beans are the backbone of the application.
  • Application Context: The central interface within the Spring application for providing configuration information to the application. It is loaded after the application has started and then passed to any CommandLineRunner beans that need to run before the application is ready to serve requests.

Similar Reads

Implementation to Run a CommandLineRunner Bean Conditionally in Spring Boot Application

Below are the implementation steps to run a CommandLineRunner Bean Conditionally in Spring Boot....

Contact Us