xvik / dropwizard-guicey

Dropwizard guice integration
http://xvik.github.io/dropwizard-guicey/
MIT License
232 stars 49 forks source link

Failing with "Unable to create injector" when resources are manually registered #379

Closed sbailliez closed 6 months ago

sbailliez commented 6 months ago

Dropwizard 3.0.6 Guicey 6.2.2

I'm migrating from dropwizard 2.1.x to 3.x and updating guicey in the process, there seems to have been some behavior changes in relation to the lifecycle and I'm a bit confused as to what it is trying to do and what is the recommended way to have compatibility with manual registration.

For example I'm using dropwizard-swagger that has a SwaggerBundle that is registering a SwaggerResource

existing application code in the service is similar to

        var swaggerBundle = new SwaggerBundle<ServiceConfiguration>() {
            @Override
            protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(ServiceConfiguration serviceConfiguration) {
                return serviceConfiguration.getSwagger();
            }
        };

        this.guiceBundle = GuiceBundle.builder()
                // Do not prioritize the Jersey providers found by Guice, instead we will explicitly register them with
                // Jersey as custom providers
                .option(InstallersOptions.PrioritizeJerseyExtensions, false)
                .enableAutoConfig(getClass().getPackage().getName())
                .modules(new ServiceModule(hibernateBundle, hibernateReporting))
                .dropwizardBundles(swaggerBundle)
                .build();
        bootstrap.addBundle(guiceBundle);

and then when I try to run it fails with

com.google.inject.CreationException: Unable to create injector, see the following errors:

1) [Guice/MissingConstructor]: No injectable constructor for type SwaggerResource.

class SwaggerResource does not have a @Inject annotated constructor or a no-arg constructor.

Requested by:
1  : SwaggerResource.class(SwaggerResource.java:22)
     at AbstractJerseyInstaller.bindInGuice(AbstractJerseyInstaller.java:68)
      \_ installed by: GuiceBootstrapModule -> InstallerModule

Learn more:
  https://github.com/google/guice/wiki/MISSING_CONSTRUCTOR

1 error

======================
Full classname legend:
======================
AbstractJerseyInstaller: "ru.vyarus.dropwizard.guice.module.installer.feature.jersey.AbstractJerseyInstaller"
GuiceBootstrapModule:    "ru.vyarus.dropwizard.guice.module.GuiceBootstrapModule"
InstallerModule:         "ru.vyarus.dropwizard.guice.module.installer.InstallerModule"
SwaggerResource:         "com.company.service.swagger.SwaggerResource"
========================
End of classname legend:
========================

    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:589)
    at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:163)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
    at com.google.inject.Guice.createInjector(Guice.java:87)
    at ru.vyarus.dropwizard.guice.injector.DefaultInjectorFactory.createInjector(DefaultInjectorFactory.java:20)
    at ru.vyarus.dropwizard.guice.module.GuiceyRunner.createInjector(GuiceyRunner.java:109)
    at ru.vyarus.dropwizard.guice.GuiceBundle.run(GuiceBundle.java:140)
    at ru.vyarus.dropwizard.guice.GuiceBundle.run(GuiceBundle.java:101)
    at io.dropwizard.core.setup.Bootstrap.run(Bootstrap.java:199)
    at io.dropwizard.testing.DropwizardTestSupport$2.run(DropwizardTestSupport.java:270)
    at io.dropwizard.core.cli.EnvironmentCommand.run(EnvironmentCommand.java:65)
    at io.dropwizard.core.cli.ConfiguredCommand.run(ConfiguredCommand.java:98)
    at io.dropwizard.testing.DropwizardTestSupport.startIfRequired(DropwizardTestSupport.java:304)
    at io.dropwizard.testing.DropwizardTestSupport.before(DropwizardTestSupport.java:204)
    at io.dropwizard.testing.junit5.DropwizardAppExtension.before(DropwizardAppExtension.java:176)
    at io.dropwizard.testing.junit5.DropwizardExtensionsSupport.beforeAll(DropwizardExtensionsSupport.java:97)
    at io.dropwizard.testing.junit5.DropwizardExtensionsSupport.beforeAll(DropwizardExtensionsSupport.java:83)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

So for some reasons it tries to recreate an instance of SwaggerResource ? Note that this code is working fine with dw 2.1.x and associated guicey. I didn't dive yet into the internals of guicey to understand what is going on with the lifecycle but going to assume you have a pretty good idea and you have seen this issue before.

sbailliez commented 6 months ago

Nevermind. Figured out the issue which was due to scanning resources.