Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the @component bean name in mapstruct mapper

Tags:

mapstruct

I know I can create a mapper like this:

@Mapper(componentModel="spring")
public interface MyMapper{

That will result in:

@Component
public class MyMapperImpl{

How can I set the bean name obtaining:

@Component("name")
public class MyMapperImpl{
like image 337
Phate Avatar asked May 30 '26 00:05

Phate


1 Answers

This is currently not supported. This is issue mapstruct/mapstruct#1427 requested for this feature.

Depending on how your name should look like you can use a custom AnnotationBeanNameGenerator to generate the mappers that are component scanned.

Starting from MapStruct 1.6 (1.6.0.Beta1 is already released) it is possible to use the new @AnnotateWith

e.g.

@AnnotateWith(value = Service.class, elements = @Element(strings = "myMapperV2")
@Mapper(componentModel="spring")
public interface MyMapper {
    // ...
}
like image 90
Filip Avatar answered May 31 '26 19:05

Filip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!