Skip to content

Implement InjectionStrategy.SETTER #3229

@MLNW

Description

@MLNW

Use case

It is possible that there is a dependency cycle between multiple mappers, e.g.:

@Mapper(componentModel = "spring",  uses = { FooMapper.class })
public interface BarMapper {
    Bar toBo(BarDto dto);
    BarDto toDto(Bar obj) ;
}

@Mapper(componentModel = "spring",  uses = { BarMapper.class })
public interface ForMapper {
    Foo toBo(FooDto dto);
    FooDto toDto(Foo obj) ;
}

In that case newer versions of SpringBoot will fail at build time. This has been reported in #2741. #2741 (comment) noted the preferred workaround, i.e., use SETTER injection instead of FIELD or CONSTRUCTOR.

Therefore, we should implement an additional InjectionStrategy.SETTER to work around issues like this.

Generated Code

@Mapper(componentModel = "spring",  uses = { FooMapper.class }, injectionStrategy = InjectionStrategy.SETTER)
public interface BarMapper {
    Bar toBo(BarDto dto);
    BarDto toDto(Bar obj) ;
}
@Generated(...)
@Component
public class FooMapperImpl implements FooMapper {

    private BarMapper barMapper;

    ...

    public void setBarMapper(BarMapper barMapper) {
        this.barMapper = barMapper;
    }

}

Possible workarounds

A work around is to use an external dependency described in the referenced issue: #2741 (comment)

MapStruct Version

MapStruct 1.5.3.Final

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions