Spring Boot应用中根据特定条件使用CommandLineRunner

PS 使用 Spring Boot 3.1.2 进行测试

1.使用@ConditionalOnProperty

仅当特定属性存在或具有特定值时,注释@ConditionalOnProperty才会创建 bean

在此示例中,仅当或文件中的CommandLineRunner属性db.init.enabled设置为 true时才会执行application.propertiesapplication.yml


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.");

  }

}
db.init.enabled=true

2、使用环境

我们可以使用Environmentbeanif语句以编程方式检查条件。

在此示例中,CommandLineRunner仅当该属性db.init.enabled设置为true时才会执行。


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. 使用弹簧配置文件

仅当特定 Spring 配置文件处于活动状态时,注释@Profile才会创建bean

在此示例中,CommandLineRunner仅当 Spring 活动配置文件为 时才会执行dev


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.");

  }
}

设置Spring 活动配置文件的不同方法。

spring.profiles.active=dev

Spring Boot Maven 插件

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

java -jar

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

4. 检查是否存在其他 Bean

仅当应用程序上下文中存在或缺少特定bean时,@ConditionalOnBean@ConditionalOnMissingBean注释才会创建bean

4.1 使用@ConditionalOnBean

@ConditionalOnBean如果特定bean存在于应用程序上下文中,则注释将创建该bean

在此示例中,仅当bean存在于应用程序上下文中CommandLineRunner时才会执行BookController


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 使用@ConditionalOnMissingBean

@ConditionalOnMissingBean如果应用程序上下文中不存在特定的bean,则注释将创建该bean

在此示例中,仅当该bean 不存在于应用程序上下文中CommandLineRunner时才会执行BookController


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) {

    //...

  }
}
posted @ 2024-03-01 09:50  Sureing  阅读(21)  评论(0编辑  收藏  举报