mapstruct 1.3.1FINAL:@Mapper(componentModel = "spring") 不支持自定义 bean 名称引用 mapstruct/mapstruct#1427

mapstruct 1.3.1FINAL: @Mapper(componentModel = "spring") doesn't support custom bean name references mapstruct/mapstruct#1427

我的问题在 github https://github.com/mapstruct/mapstruct/issues/1427

上引用了这个问题

我至少有两个版本的同名映射器。我想使用 springs getBean/Autowired 的可能性,但这还不能脱离 mapstructs 框。 :-)

我遵循了上面link中提到的第二种解决方法:扩展Springs bean命名策略。有人曾经让这个福利计划奏效吗?

如果我按照那里的代码部分进行操作,则不会发生 bean 命名。对我来说,为什么不这样做很清楚:没有任何组件可以扫描,尤其是可以找到。

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

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

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

然后您可以使用 @AutowiredgetBean.

注入映射器

正如 Filip 在此处所说 https://github.com/mapstruct/mapstruct/issues/1427 我遵循了他的方法,并进行了一些修改,它奏效了。我在 link.

中添加了解决方案评论

主要变化是:

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

  2. 我在 Spring 启动应用程序 class 添加了:

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