1

I want to use dependency inject only in certain part of my code. I am using HubSpot/dropwizard-guice to integrate drop-wizard and guice. Is there a way I can access guice object instances programmatically without changing the full project. Basically I would replace initialised objects with guice objects, So that I don't have to change everything at once.

Here is my application file

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {

  public static void main(String[] args) throws Exception {
    new HelloWorldApplication().run(args);
  }

  @Override
  public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {

    GuiceBundle<HelloWorldConfiguration> guiceBundle = GuiceBundle.<HelloWorldConfiguration>newBuilder()
      .addModule(new HelloWorldModule())
      .enableAutoConfig(getClass().getPackage().getName())
      .setConfigClass(HelloWorldConfiguration.class)
      .build();

    bootstrap.addBundle(guiceBundle);
  }

  @Override
  public String getName() {
    return "hello-world";
  }

  @Override
  public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
  }
}

Is there a way I can access guice injector without explicitly passing it as a method param all over my project?

Or Is there a way to directly get class instances from guice.

Marius Jaraminas
  • 813
  • 1
  • 7
  • 19
best wishes
  • 5,789
  • 1
  • 34
  • 59
  • 1
    I would make a singleton that wraps your guiceBundle object and initialize it at startup(or lazy if needed), then just call that wrapper. – Nigel Savage Jan 09 '20 at 14:52

0 Answers0