12

I'm in process of learning Guice and I don't clearly understand how to use Injector instance. It's better to create Injector instance once on application bootstrap, and make it public singleton?

And is it true that we always must use Injector#getInstance(SomeClass.class) to get classes where we putted Guice's @Inject annotations?

WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • It's better to not use `Injector.getInstance()` at all. – millimoose Feb 12 '13 at 19:20
  • 2
    @millimoose but what is other ways to retrieve classes managed by Guice? – WelcomeTo Feb 12 '13 at 19:34
  • Fields annotated with `@Inject` should have the right classes available "automagically". (Assuming you have Guice manage the classes that need injecting too.) The idea is that you have the container manage both your dependencies, **and** the classes that use them, and only fetch one "main" class from the injector manually to start your app. You should also consider [rereading the tutorial](https://code.google.com/p/google-guice/wiki/GettingStarted), this is Dependency Injection 101. – millimoose Feb 12 '13 at 19:37
  • 1
    Yes, I read it before asking question, but class that has fields annotated with `@Inject` must be retrieved using `Injector#getInstance(MyClass.class)`. So what if I have multiple classes with fields annotated with `@Inject`? I need to initiate every class using `Injector#getInstance(ClassName.class)`. – WelcomeTo Feb 12 '13 at 19:55
  • Ah. Not necessarily. Only if you need to call a method on one from a class that's not managed by Guice. Alternately you can instead do `Injector.injectMembers(this)` and populate all fields annotated with `@Inject` from the `Injector` even on an unmanaged object. – millimoose Feb 12 '13 at 20:01
  • Yes, I call guice-managed class from class which don't know anything about guice (so it don't work). I tested `Injector.injectMembers(this)`. As I understand I need to call this method in every class which has `@Inject` annotations? – WelcomeTo Feb 12 '13 at 20:34
  • No, only for objects that aren't Guice-managed. – millimoose Feb 12 '13 at 22:37

1 Answers1

9

You should not pass the injector around as a global singleton. Have you looked at: https://github.com/google/guice/wiki/GettingStarted? Note that RealBillingService does not use the injector to get instances of CreditCardProcessor and TransactionLog. Instead Guice handles all this for you when creating the instance.

If you're in a situation where you need Guice to create many objects of the same type consider using a Provider and injecting that provider.

Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
condit
  • 10,852
  • 2
  • 41
  • 60
  • Yes, I understand it. But what if I have multiple services like `RealBillingService`, and each of them have `@Inject` annotations for injecting `CreditCardProcessor` and `TransactionLog` objects. So I need use `Injector#getInstance` for every service like `RealBillingService`. But I want to create this services in different places, so I have 2 variants: create `Injector` object in every place and use `Injector#getInstance` for retrieving neccessary service _or_ to create `Injector` instance once make it accessible (global singleton) to all places where I need it. – WelcomeTo Feb 12 '13 at 19:32
  • Or have a third object that has the instances of your two services (both annotated with `@Inject`). Then you would use the `Injector` to get the single instance of this third object. – condit Feb 12 '13 at 19:44
  • In practice you rarely see the `injector`. If you're using `Jersey` with `Guice`, for instance, you configure your Modules and then the framework handles the rest. – condit Feb 12 '13 at 19:47
  • i.e. Jersey have support for Guice out of the box? It like: Jersey is Guice-managed component, so we don't need to explicitly use `Injector` instance? – WelcomeTo Feb 12 '13 at 19:52
  • Or you mean Jersey in context of JEE container? I.e. CDI (Guice) can be used only in container-manager components, like EJB, JAX-RS (Jersey), JAX-WS etc without explicitly using `Injector#getInstance`? – WelcomeTo Feb 12 '13 at 19:58
  • Not out of the box. You need to add a dependency to `jersey-guice`. See an example here: http://randomizedsort.blogspot.com/2011/05/using-guice-ified-jersey-in-embedded.html and note that the Injector is not used. – condit Feb 12 '13 at 19:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24396/discussion-between-condit-and-mytitle) – condit Feb 12 '13 at 20:00