Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inject Spring CustomComponent in Mapper interface? #84

Closed
albilu opened this issue Aug 6, 2023 · 2 comments
Closed

How to inject Spring CustomComponent in Mapper interface? #84

albilu opened this issue Aug 6, 2023 · 2 comments

Comments

@albilu
Copy link

albilu commented Aug 6, 2023

I am trying to achieve the following but not sure if this is what this extensions is meant for. I am a bit confused.

I will like to inject a custom service in the CarMapper interface extending the Spring Converter.

It seems injection is not possible with CarMapper interface but only with abstract classes.

Is it possible to achieve this while keeping the integration with the Spring ConversionService?

@Mapper(componentModel = "spring")
public interface CarMapper extends Converter<Car, CarDto> {

    @Autowired
    private MyCustomService myCustomService;//This is not working

    @Mapping(target = "title", expression = "java(getLocaleTitle(car)))
    CarDto convert(Car car);
    

    default String getLocaleTitle(Car car) {
        return myCustomService.translate(car.getTitle());
    }
}

Usage:

@Autowired
    private ConversionService conversionService;
...
    Car car = ...;
    CarDto carDto = conversionService.convert(car, CarDto.class);
@Chessray
Copy link
Collaborator

Chessray commented Aug 6, 2023

Why don't you make the Mapper an abstract class instead? By the way, this doesn't require the extension at all. As the documentation says:

All this can be achieved already with MapStruct’s core functionality.

With or without the extension, nothing prevents you from writing

public abstract class CarMapper implements Converter<Car, CarDto>

@Chessray Chessray closed this as completed Aug 6, 2023
@albilu
Copy link
Author

albilu commented Aug 6, 2023

@Chessray Thanks you it did work. Sorry for the confusion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants