Prince of Sweden
Prince of Sweden

Reputation: 475

Mapstruct generate Constructor for dependency injection

I'm trying to generate an implementation in MapStruct that will create a constructor for me that I can use for constructor-based dependency injection. I have learned that I can't use constructor-injection in the mapper definition(seen below), but how do I make it so that my generated class has one?

I have tried below:

@Mapper(componentModel = "spring", uses = Dependency.class, injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public abstract class MapStructTest {
private Dependency dependency;

@Mapping(source = "field", target "target")
@Mapping(target = "target2", ignore = true)
@AfterMapping
public final void runAfter() {
//dostuff for target2
    }
}

With no success. My class is generated, looks ok, except there is no constructor. How do I define my mapper so that I get a constructor I can work with in the implementation?

Best regards, Prince of Sweden

Upvotes: 4

Views: 6600

Answers (1)

Filip
Filip

Reputation: 21423

MapStruct does not support calling constructors for abstract classes. You have to have a default empty constructor.

In order to inject Dependency you can use @Autowired on your field or add a setter with @Autowired

Upvotes: 7

Related Questions