boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

Spring Boot 条件 Bean 加载详解


avatar
作者 2025年9月7日 12

Spring Boot 条件 Bean 加载详解

本文旨在解决 spring Boot 项目中条件性加载 Bean 的问题,通过 @ConditionalOnProperty 注解,可以根据配置文件的属性值来决定是否加载特定的 Bean。我们将提供一个完整的示例,展示如何根据 application.yml 配置文件中的 use 属性来动态加载不同的 Component 配置,并确保只有满足条件的 Bean 才会被实例化和注入。

spring boot 项目中,我们经常需要根据不同的环境或配置来加载不同的 Bean。@ConditionalOnProperty 注解提供了一种优雅的方式来实现这一点。下面,我们将通过一个实际的例子,详细讲解如何使用 @ConditionalOnProperty 来条件性地加载配置 Bean。

1. 定义配置属性类

首先,我们需要定义一个 ComponentConfigPart 类,用于存储组件的配置属性:

import lombok.Data; import lombok.NoArgsConstructor;  @Data @NoArgsConstructor public class ComponentConfigPart {     private String ex1;     private String ex2;     private String ex3; }

接下来,创建一个抽象的 ComponentConfig 类,作为所有组件配置类的基类。这个类包含一个 ComponentConfigPart 列表,并使用 @PostConstruct 注解定义了一个初始化方法,用于在 Bean 创建后打印日志:

import lombok.Data; import Javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List;  @Data public abstract class ComponentConfig {     private List<ComponentConfigPart> parts = new ArrayList<>();      @PostConstruct     public void init() {         System.out.println("Created instance of " + this.getClass().getSimpleName());         System.out.println("Created " + this);     } }

2. 创建组件配置类

现在,我们可以创建具体的组件配置类,例如 ComponentAConfig 和 ComponentBConfig。这些类继承自 ComponentConfig,并使用 @ConditionalOnProperty 注解来指定加载条件。

ComponentAConfig:

import lombok.ToString; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;  @Component @ConfigurationProperties(prefix = "application.components.a") @ConditionalOnProperty(prefix = "application", name = "use", havingValue = "componentA") @ToString(callSuper = true) public class ComponentAConfig extends ComponentConfig {  }

ComponentBConfig:

import lombok.ToString; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;  @Component @ConfigurationProperties(prefix = "application.components.b") @ConditionalOnProperty(prefix = "application", name = "use", havingValue = "componentB") @ToString(callSuper = true) public class ComponentBConfig extends ComponentConfig {  }

注意以下几点:

Spring Boot 条件 Bean 加载详解

Adobe Firefly

Adobe最新推出的ai图像生成和编辑工具

Spring Boot 条件 Bean 加载详解32

查看详情 Spring Boot 条件 Bean 加载详解

  • @Component 注解将这些类声明为 Spring Bean。
  • @ConfigurationProperties 注解指定了配置属性的前缀,例如 application.components.a 和 application.components.b。
  • @ConditionalOnProperty(prefix = “application”, name = “use”, havingValue = “componentA”) 指定了只有当 application.use 属性的值为 componentA 时,才会加载 ComponentAConfig Bean。ComponentBConfig 同理。
  • @ToString(callSuper = true) 注解使用了 Lombok 库,用于生成包含父类属性的 toString() 方法,方便调试。

3. 创建主配置类

创建一个 MainConfig 类,用于自动注入 ComponentConfig Bean。Spring 会根据 application.use 属性的值,自动注入相应的组件配置 Bean。

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct;  @Configuration public class MainConfig {      @Autowired     private ComponentConfig config;      @PostConstruct     public void init() {         System.out.println("MainConfig has autowired class of " + config.getClass().getSimpleName());     } }

4. 配置 application.yml

在 application.yml 文件中,配置组件的属性和 use 属性:

application:   components:     a:       parts:         - ex1: a           ex2: aa           ex3: aaa         - ex1: a2           ex2: aa2           ex3: aaa2     b:       parts:         - ex1: b           ex2: bb           ex3: bbb         - ex1: b2           ex2: bb2           ex3: bbb2   use: componentA

5. 运行结果

当 application.use 的值为 componentA 时,控制台输出如下:

Created instance of ComponentAConfig Created ComponentAConfig(super=ComponentConfig(parts=[ComponentConfigPart(ex1=a, ex2=aa, ex3=aaa), ComponentConfigPart(ex1=a2, ex2=aa2, ex3=aaa2)])) MainConfig has autowired class of ComponentAConfig

当 application.use 的值为 componentB 时,控制台输出如下:

Created instance of ComponentBConfig Created ComponentBConfig(super=ComponentConfig(parts=[ComponentConfigPart(ex1=b, ex2=bb, ex3=bbb), ComponentConfigPart(ex1=b2, ex2=bb2, ex3=bbb2)])) MainConfig has autowired class of ComponentBConfig

总结

通过以上步骤,我们成功地实现了 Spring Boot 项目中条件性加载 Bean 的功能。@ConditionalOnProperty 注解可以根据配置文件的属性值来动态加载不同的 Bean,使得我们的应用程序更加灵活和可配置。

注意事项:

  • 确保 @ConditionalOnProperty 注解的 prefix 和 name 属性与 application.yml 文件中的属性名匹配。
  • @ConfigurationProperties 注解的前缀要与 application.yml 文件中的配置结构对应,以便正确地绑定配置属性。
  • 如果使用了 Lombok 库,需要确保 ide 已经安装了 Lombok 插件,以便正确地生成代码。
  • 在实际项目中,可以根据需要扩展 ComponentConfig 类,添加更多的配置属性。

希望本教程能够帮助你理解和应用 Spring Boot 的条件 Bean 加载功能。



评论(已关闭)

评论已关闭