Multiple constructors

When you declare multiple constructors for your service, by default, the dependency injector cannot decide which one to use to initialize the service with.

In order to fix this problem, annotate the desired constructor with the @ConstructWith annotation, to tell the dependency injector, which constructor to use.

@Service
class ImageProcessor {
    private final int quality;
    private final boolean resize;
    
    // by default, the dependency injector will use this method to 
    // instantiate the `ImageProcessor` class
    @ConstructWith
    public ImageProcessor(ImageOptions options) {
        quality = options.getQuality();
        resize = options.getResize();
    }
    
    // you may have various constructors, as you wish
    public ImageProcessor(int quality, boolean resize) {
        this.quality = quality;
        this.resize = resize;
    }
}

Last updated