[英]MapStruct with Spring Boot, annotate generated classes with custom annotation
Is there possibility, to annotate class generated by MapStruct, with custom annotation?是否有可能使用自定义注释来注释 MapStruct 生成的类? Currently Im using MapStruct with componentModel
defined as "spring"
, like:目前我使用 MapStruct 和componentModel
定义为"spring"
,如:
@Mapper(componentModel = "spring")
public interface MyMapper {}
For this interface, class is generated by MapStruct:对于这个接口,类由 MapStruct 生成:
@Component
public class MyMapperImpl implements MyMapper {}
So I can easily inject it.所以我可以很容易地注入它。 What I want to do, is to annotate generated class with org.springframework.context.annotation.Profile
annotation, so my generated bean will be in force only if specific profile is defined, is there possibility to inject such annotation, @Profile("dev")
, to generated class somehow?我想要做的是用org.springframework.context.annotation.Profile
注释来注释生成的类,所以我生成的 bean 只有在定义了特定的配置文件时才会生效,是否有可能注入这样的注释,@ @Profile("dev")
,以某种方式生成类? I was thinking about org.mapstruct.DecoratedWith
annotation, but I didn't make it working for me我在考虑org.mapstruct.DecoratedWith
注释,但我没有让它为我工作
This is currently not possible with the official API.目前官方 API 无法做到这一点。 This question is similar to mapstruct/mapstruct#1427 .这个问题类似于mapstruct/mapstruct#1427 。
However, there are 2 options that you can try to make it work.但是,您可以尝试使用 2 个选项使其工作。
You can try to annotate MyMapper
with the @Profile
annotation, I am not sure whether Spring searches up the inheritance tree and in interface or not您可以尝试使用@Profile
注释来注释MyMapper
,我不确定 Spring 是否在继承树和接口中搜索
MapStruct has the ModelElementProcessor
that it used to add the @Component
annotation. MapStruct有ModelElementProcessor
它用于添加@Component
注解。 The one for Spring is SpringComponentModelElementProcessor
. Spring 的一个是SpringComponentModelElementProcessor
。
You can provide your own componentModel
( spring-dev
for example) that would add the @Profile
annotation to the generated mapper.您可以提供自己的componentModel
(例如spring-dev
),将@Profile
注释添加到生成的映射器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.