如何根据条件指定要初始化的bean

问题描述 投票:0回答:0

我有一个必须基于 config.property 值创建的工厂

  @Service
  @RequiredArgsConstructor
  @ConditionalOnProperty("'${config.property}==a' or '${config.property}==b'")
  public class CustomFactory {
    private final SpecificActionOnA specificActionOnA;
    private final SpecificActionOnB specificActionOnB;
 } 

为了成功创建

CustomFactory
类型的bean,我必须将所有条件传递给每个SpecificAction组件
SpecificActionOnA
SpecificActionOnB
的签名分别是

@Component
@ConditionalOnProperty("'${config.property}==a'or '${config.property}==b'")
public class SpecificActionOnA {}

@Component
@ConditionalOnProperty("'${config.property}==a' or '${config.property}==b'")
public class SpecificActionOnB {}

我的期望是为我的 SpecificAction 定义这个定义,它不会分别在条件 b 和 SpecificActionOnB 上加载 SpecificActionOnA。

@Component
@ConditionalOnProperty("'${config.property}==a'")
public class SpecificActionOnA {}

@Component
@ConditionalOnProperty("'${config.property}==b'")
public class SpecificActionOnB {}

由于与工厂的差异太小,我无法为每个案例创建一个特定的工厂

spring spring-boot javabeans
© www.soinside.com 2019 - 2024. All rights reserved.