英文:
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
我至少有两个同名版本的映射器。我想使用Spring的getBean/Autowired功能,但这在mapstruct的框架之外不起作用。
我遵循了上面链接中提到的第二种解决方法:扩展Spring的bean命名策略。有人能够让这个善意的提议成功吗?
如果我按照那里的代码部分进行,bean的命名就不会生效。对我来说,为什么不生效是很清楚的:没有任何组件需要扫描,尤其是没有需要找到的组件。
如果我在映射器注释中添加componentModel = "spring"
,我会得到一个ConflictingBeanDefinitionException
。不知道为什么。也许有一个尾部问题,类似于"猫在尾巴上"的问题?
英文:
My problem references to this issue here on github <https://github.com/mapstruct/mapstruct/issues/1427>
I got at least two versions of mappers with the same name. I want to use springs getBean/Autowired possibilities but this doesn't work out of the mapstructs box yet.
I followed the second workaround mentioned in the upper link: extend Springs bean naming strategy. Did someone ever get this well ment proposal working?
If i follow the code parts from there the bean naming doesn't take place. For me its's clear why not: there aren't any components to scan and especially to find.
If i add a componenModel = "spring" to the mapper annotation i get a ConflictingBeanDefinitionException. Don't know why. Maybe there's a cat in the tail problem?
答案1
得分: 2
按照Filip在这里的说明https://github.com/mapstruct/mapstruct/issues/1427,我遵循了他的方法,并进行了一些修改使其生效。我在链接中添加了解决方案的评论。
主要更改如下:
-
我在我的映射器中添加了
componentModel = "spring"
,并使用过滤器来排除在Spring Boot应用程序中实现了我所有映射器的接口(MapperInterface.class)的所有映射器类。 -
我在我的Spring Boot应用程序类中添加了:
@ComponentScan(basePackages = { "com.application.spring_boot_class" }, excludeFilters = { @ComponentScan.Filter(value = { MapperInterface.class }, type = FilterType.ASSIGNABLE_TYPE) })
英文:
As stated from Filip here https://github.com/mapstruct/mapstruct/issues/1427 i followed his approach and with a few modifications it worked. I added a solution comment in the link.
The main changes are:
-
i added
componentModel = "spring"
to my mappers and used a filter to exclude all of my mapper classes (the interface all of my mappers are implementing: MapperInterface.class) within the Spring Boot application. -
To my Spring Boot application class i added:
@ComponentScan(basePackages = { "com.application.spring_boot_class" }, excludeFilters = { @ComponentScan.Filter(value = { MapperInterface.class }, type = FilterType.ASSIGNABLE_TYPE) })
答案2
得分: 1
我之前遇到过类似的问题,我是通过在一个配置类中使用Spring bean
的定义来解决的,这个配置类是用@Configuration
注解标注的,代码如下所示:
@Bean
public IMyMapper offerWebMapper() {
return Mappers.getMapper(IMyMapper.class);
}
然后你可以使用@Autowired
或者getBean
来注入这个映射器。
英文:
I had this issue before, and I resolved it using the Spring bean
definition in a configuration class, a class annotated with @Configuration
, with the Mapstruct
mapper call like below:
@Bean
public IMyMapper offerWebMapper() {
return Mappers.getMapper(IMyMapper.class);
}
And then you can inject the mapper using @Autowired
or getBean
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论