Main Tutorials

How to Run a CommandLineRunner Bean Conditionally in Spring Boot

This article shows different ways to run a CommandLineRunner bean conditionally in a Spring Boot application.

Table of contents:

P.S. Tested with Spring Boot 3.1.2

1. Using @ConditionalOnProperty

The @ConditionalOnProperty annotation creates the bean only when a specific property exists or has a particular value.

In this example, the CommandLineRunner will only execute if the property db.init.enabled is set to true in the application.properties or application.yml file.

DatabaseInitializer.java

package com.mkyong;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(
      name = "db.init.enabled",
      havingValue = "true",
      matchIfMissing = false
)
public class DatabaseInitializer implements CommandLineRunner {

  @Override
  public void run(String... args) {

      System.out.println("This runs when 'db.init.enabled' property is true.");

  }

}
application.properties

db.init.enabled=true

2. Using Environment

We can programmatically check conditions using the Environment bean and if statements.

In this example, the CommandLineRunner will only execute if the property db.init.enabled is set to true.

DatabaseInitializer.java

package com.mkyong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class DatabaseInitializer implements CommandLineRunner {

  @Autowired
  private Environment env;

  @Override
  public void run(String... args) {

      if ("true".equals(env.getProperty("db.init.enabled"))) {
          System.out.println("This runs when 'db.init.enabled' property is true.");
      }

  }
}

3. Using Spring Profiles

The @Profile annotation creates the bean only when a specific Spring profile is active.

In this example, the CommandLineRunner will only execute if the Spring active profile is dev.

DatabaseInitializer.java

package com.mkyong;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("dev")
public class DatabaseInitializer implements CommandLineRunner {

  @Override
  public void run(String... args) {

      System.out.println("This runs when profile is to dev.");

  }
}

Different ways to set the Spring active profiles.

application.properties

spring.profiles.active=dev

Spring Boot Maven Plugin

Terminal

  ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev  

java -jar

Terminal

java -jar -Dspring.profiles.active=dev target/spring-boot-commandlinerunner-1.0.jar

4. Check the Presence of Other Beans

The @ConditionalOnBean and @ConditionalOnMissingBean annotations create the bean only when a specific bean is present or missing in the application context.

4.1 Using @ConditionalOnBean

The @ConditionalOnBean annotation creates the bean if the specific bean is present in the application context.

In this example, the CommandLineRunner will only execute if the BookController bean is present in the application context.

DatabaseInitializer.java

package com.mkyong;

import com.mkyong.book.BookController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnBean(BookController.class)
public class DatabaseInitializer implements CommandLineRunner {

  @Override
  public void run(String... args) {

    //...

  }
}

4.2 Using @ConditionalOnMissingBean

The @ConditionalOnMissingBean annotation creates the bean if the specific bean is NOT present in the application context.

In this example, the CommandLineRunner will only execute if the BookController bean is NOT present in the application context.

DatabaseInitializer.java

package com.mkyong;

import com.mkyong.book.BookController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnMissingBean(BookController.class)
public class DatabaseInitializer implements CommandLineRunner {

  @Override
  public void run(String... args) {

    //...

  }
}

5. Download Source Code

$ git clone https://github.com/mkyong/spring-boot.git

$ cd spring-boot-commandlinerunner

6. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
vRo
1 month ago

thank you You are post is aways helpful to me

Ravi
3 months ago

Helpful