Spring Boot 如何在Mapstruct中使用@Conditional

ulydmbyx  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(217)

我想使用Sping Boot @Conditional和Mapstruct。
我想使用属性文件限制Spring Bean的创建。我使用@Conditional进行相同的操作。
如何为mapstruct bean实现它?

9rygscc1

9rygscc11#

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

  1. @Configuration
  2. class ConditionalBeanConfiguration {
  3. @Bean
  4. @Conditional... <--
  5. ConditionalBean conditionalBean(){
  6. return new ConditionalBean();
  7. };
  8. }

字符串
所以它可能看起来像:

  1. @Configuration
  2. class MapperBeanConfiguration {
  3. @Bean
  4. @ConditionalOnProperty(name = "my.property.enabled", havingValue = "true")
  5. public MyMapper myMapperImplV1() {
  6. return new MyMapperImplV1();
  7. }
  8. @Bean
  9. @ConditionalOnProperty(name = "my.property.enabled", havingValue = "false", matchIfMissing = true)
  10. public MyMapper myMapperImplV2() {
  11. return new MyMapperImplV2();
  12. }
  13. }


你可以在这篇文章中了解更多:working with springs conditional annotation for conditional bean registration

展开查看全部

相关问题