innoveit / play2-pdf

A PDF module for Play Framework 2
MIT License
77 stars 22 forks source link

Use constructor injection instead of field injection #32

Closed eximius313 closed 5 years ago

eximius313 commented 5 years ago

PdfGenerator uses filed injection:

@Inject
Environment environment;

public PdfGenerator() {

}

therefore there is no possibility to configure it at all, because if you do:

public class ApplicationModule extends AbstractModule {
  @Override
  protected void configure() {
    bind(PdfGenerator.class).toProvider(PdfGeneratorProvider.class).asEagerSingleton();
  }

  static class PdfGeneratorProvider implements Provider<PdfGenerator> {
    private final PdfGenerator pdfGenerator;

    public PdfGeneratorProvider() {
      this.pdfGenerator = new PdfGenerator();
      this.pdfGenerator.loadLocalFonts(asList("fonts/opensans-regular.ttf"));
    }

    @Override
    public PdfGenerator get() {
      return pdfGenerator;
    }
  }
}

then you end up in NullPointerException (because environment is null!)

If you just change it to construction injection:

final Environment environment;

@Inject
public PdfGenerator(final Environment environment) {
        this.environment = environment;
}

then everything works and is backward compatibile

marcosinigaglia commented 5 years ago

Hi, I had a problem with Nexus , the release is 1.8.2.

eximius313 commented 5 years ago

Allright! I'll check it today evening and provide PR for 1.9.x

eximius313 commented 5 years ago

here it is: https://github.com/innoveit/play2-pdf/pull/36