Mapstruct - How can I inject a spring dependency in the Generated Mapper class

后端 未结 5 733
慢半拍i
慢半拍i 2020-12-05 17:52

I need to inject a spring service class in the generated mapper implementation, so that I can use it via

   @Mapping(target=\"x\", expression=\"java(myservi         


        
5条回答
  •  不思量自难忘°
    2020-12-05 18:32

    It should be possible if you declare Spring as the component model and add a reference to the type of myservice:

    @Mapper(componentModel="spring", uses=MyService.class)
    public interface MyMapper { ... }
    

    That mechanism is meant for providing access to other mapping methods to be called by generated code, but you should be able to use them in the expression that way, too. Just make sure you use the correct name of the generated field with the service reference.

提交回复
热议问题