spring Sping Boot 条件加载Bean

63lcw9qa  于 2022-11-21  发布在  Spring
关注(0)|答案(2)|浏览(188)

我正在运行一个Spring Boot 项目,我正在尝试有条件地加载一些bean。我有以下配置文件:

application:
    components:
        componentA:
            - ex1
              ex2
              ex3
        componentB:
            - ex1
              ex2
              ex3
        componentC:
            - ex1
              ex2
              ex3
    use: componentA

和以下配置类
第一个
即使我在每个配置类上定义了**@ConditionalOnProperty**,它仍然会加载所有配置类。我如何才能只加载componentA的列表而忽略其他2个配置类?谢谢

lymnna71

lymnna711#

我想,我会这样做的。(根据后来的评论编辑)
配置部件类:

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

Bean的基类

@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);
    }
}

ComponentA类别

@Component
@ConfigurationProperties(prefix = "application.components.a")
@ConditionalOnProperty(prefix = "application", name = "use", havingValue = "componentA")
@ToString(callSuper = true)
public class ComponentAConfig extends ComponentConfig {
    
}

ComponentB类别:

@Component
@ConfigurationProperties(prefix = "application.components.b")
@ConditionalOnProperty(prefix = "application", name = "use", havingValue = "componentB")
@ToString(callSuper = true)
public class ComponentBConfig extends ComponentConfig {

}

以及MainConfig,它希望获得“选定”组件:

@Configuration
public class MainConfig {

    @Autowired
    private ComponentConfig config;

    @PostConstruct
    public void init() {
        System.out.println("MainConfig has autowired class of " + config.getClass().getSimpleName());
    }
}

现在,属性如下所示:

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

并在控制台日志中显示结果:

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:
  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: 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
o8x7eapl

o8x7eapl2#

这就是我所寻找的行为,但每个组件都应该是这样的列表:

application:
  components:
    componentA:
        - ex1
          ex2
          ex3
        - ex1
          ex2
          ex3
    componentB:
        - ex1
          ex2
          ex3
        - ex1
          ex2
          ex3
    componentC:
        - ex1
          ex2
          ex3
        - ex1
          ex2
          ex3
use: componentA

这就是为什么我有MainConfig类与每个列表。

相关问题