如何将 Spring Boot @Conditioanl 与 Mapstruct 一起使用

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

我想将 Spring Boot @Conditioanl 与 Mapstruct 一起使用。

我想使用属性文件限制 spring bean 的创建。 我正在使用 @Conditional 来实现同样的目的。

如何在mapstruct中实现它。

spring-boot mapstruct
1个回答
0
投票

您可以使用条件 bean 创建一个 Configuration 类:

@Configuration
class ConditionalBeanConfiguration {

  @Bean
  @Conditional... <--
  ConditionalBean conditionalBean(){
    return new ConditionalBean();
  };

}

所以它可能看起来像:

@Configuration
class MapperBeanConfiguration {

    @Bean
    @ConditionalOnProperty(name = "my.property.enabled", havingValue = "true")
    public MyMapper myMapperImplV1() {
        return new MyMapperImplV1();
    }

    @Bean
    @ConditionalOnProperty(name = "my.property.enabled", havingValue = "false", matchIfMissing = true)
    public MyMapper myMapperImplV2() {
        return new MyMapperImplV2();
    }

}

您可以在这篇中等文章中了解更多信息:使用 Spring 条件注释进行条件 bean 注册

© www.soinside.com 2019 - 2024. All rights reserved.