【问题标题】:mapstruct 1.3.1FINAL: @Mapper(componentModel = "spring") doesn't support custom bean name references mapstruct/mapstruct#1427mapstruct 1.3.1FINAL:@Mapper(componentModel = "spring") 不支持自定义 bean 名称引用 mapstruct/mapstruct#1427
【发布时间】:2020-09-14 21:30:52
【问题描述】:

我的问题在 github https://github.com/mapstruct/mapstruct/issues/1427 上引用了这个问题

我至少有两个版本的映射器具有相同的名称。我想使用 springs getBean/Autowired 的可能性,但这还不能在 mapstructs 框中起作用。 :-)

我遵循了上面链接中提到的第二种解决方法:扩展 Springs bean 命名策略。有没有人让这个健康建议奏效?

如果我从那里遵循代码部分,则不会发生 bean 命名。对我来说,为什么不这样做很清楚:没有任何要扫描的组件,尤其是要查找的组件。

如果我在映射器注释中添加一个 componenModel = "spring",我会得到一个 ConflictingBeanDefinitionException。不知道为什么。也许是猫在尾巴问题?

【问题讨论】:

    标签: java mapstruct


    【解决方案1】:

    正如 Filip 在这里https://github.com/mapstruct/mapstruct/issues/1427 所说,我遵循了他的方法,并进行了一些修改,它起作用了。我在链接中添加了解决方案评论。

    主要变化有:

    1. 我在我的映射器中添加了componentModel = "spring",并使用过滤器在 Spring Boot 应用程序中排除了我所有的映射器类(我的所有映射器都实现的接口:MapperInterface.class)。

    2. 添加到我的 Spring Boot 应用程序类中:

      @ComponentScan(basePackages = { "com.application.spring_boot_class" }, excludeFilters = { @ComponentScan.Filter(value = { MapperInterface.class }, type = FilterType.ASSIGNABLE_TYPE) })

    【讨论】:

      【解决方案2】:

      我之前遇到过这个问题,我使用配置类中的Spring bean 定义解决了这个问题,该类用@Configuration 注释,Mapstruct 映射器调用如下:

      @Bean
      public IMyMapper offerWebMapper() {
          return Mappers.getMapper(IMyMapper.class);
      }
      

      然后您可以使用@AutowiredgetBean 注入映射器。

      【讨论】:

      • 谢谢 Ismail,另一个不错的方法。希望 Filip(来自 mapstruct)在这里发表他的意见 :-)
      • ...我想说,我真的很喜欢 mapstruct。希望将来会有办法让它发挥作用。我会说这个用例是一个非常典型的用例......
      • 是的,很典型,我试过用另一种方式,但发现非常好用也很简单。
      • 我强烈建议不要这样做。特别是如果您在映射器之间使用依赖关系。当你不使用 spring 组件模型时,MapStruct 将使用Mapper.getMapper 来获取它需要重用的映射器。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 2019-12-04
      • 2019-06-17
      • 1970-01-01
      相关资源
      最近更新 更多