Gadam
Gadam

Reputation: 3024

How to use Guice Module to instantiate one of multiple fields in a class?

I have a class, and create an instance using a regular constructor:

class Foo {
  String fooName;
  Bar barObject;
  ExternalService externalService;

  Foo(String fooName, Bar barObject, Service someService){
    this.fooName = fooName;
    this.barObject = barObject;
    this.externalService = externalService;
    //call to super
    }
}


class MyApplication {
  //instantiate ExternalService 
  ...

  Foo foo = new Foo(String fooName, Bar barObject, ExternalService externalService);
}

ExternalService is owned by somebody else and they have now provided a Guice Module (something like ExternalServiceModule). How can I make use of this module to instantiate ExternalService in my Foo class?

I am trying something like

class Foo {
  String fooName;
  Bar barObject;
  ExternalService externalService;

  @Inject
  Foo(String fooName, Bar barObject, Service someService){
    this.fooName = fooName;
    this.barObject = barObject;
    this.externalService = externalService;
    //call to super
  }
}

and

class MyApplication {
    ...
  ExternalServiceModule ecm = new ExternalServiceModule();
  Injector injector = Guice.createInjector(ecm);
  Foo foo = injector.getInstance(Foo.class);
}

But obviously I am not passing the fooName, and barObject in the second way. How to do this?

Thanks.

Upvotes: 2

Views: 3271

Answers (1)

dting
dting

Reputation: 39287

If I am understanding you correctly, you are just trying to get an instance of ExternalService to pass to your Foo class constructor. You can call injector.getInstance(ExternalService.class):

class MyApplication {
  Injector injector = Guice.createInjector(new ExternalServiceModule());
  ExternalService externalService = injector.getInstance(ExternalService.class);
  Bar someBar = new Bar();
  Foo foo = new Foo('someName', someBar, externalService);
}

But since you are using Guice you probably are looking for assisted inject:

Foo

public class Foo {
  private String fooName;
  private Bar barObject;
  private ExternalService externalService;

  @Inject
  public Foo(
      @Assisted String fooName,
      @Assisted Bar barObject,
      ExternalService externalService) {
    this.fooName = fooName;
    this.barObject = barObject;
    this.externalService = externalService;
  }

  public interface FooFactory {
    Foo create(String fooName, Bar barObject);
  }
}

MyModule

public class MyModule extends AbstractModule {
  @Override
  protected void configure() {
    install(new FactoryModuleBuilder().build(FooFactory.class));
  }
}

MyApplication

public class MyApplication {

  public static void main(String[] args) {
    Injector injector = Guice.createInjector(
        new ExternalServiceModule(), new MyModule());
    FooFactory fooFactory = injector.getInstance(FooFactory.class);

    Bar someBar = new Bar();
    Foo foo = fooFactory.create("SomeName", someBar);
  }
}

Upvotes: 2

Related Questions