> > I didn't quite follow what said here: > > You typically shouldn't use Injector directly after the initial lookup. > When you depend on types, Guice can check your dependencies up front. When > you get objects directly form the Injector, you don't find out about missing > deps until run time. > > The Guice example ( > http://code.google.com/p/google-guice/wiki/GettingStarted) does exactly > that - accessing from Injector: > > public static void main(String[] args) { > > /* > * Guice.createInjector() takes your Modules, and returns a new Injector > * instance. Most applications will call this method exactly once, in > their > * main() method. > */ > > Injector injector = Guice.createInjector(new BillingModule()); > > /* > * Now that we've got the injector, we can build objects. > */ > RealBillingService billingService = > injector.getInstance(RealBillingService.class); > > ... > } > > How else do you get the injectors started and create the bound services?
You should generally only use the Injector once, to get the object that acts as the root of your application. It gets its dependencies injected in to it and they get their dependencies injected in to them, etc. without any reference to the Injector in any of them. You can then call some method on that object to get your application running. Of course, exactly what you do differs depending on how you're using Guice... if you're using guice-servlet, for example, you just create the Injector and let guice-servlet handle the rest without ever calling getInstance on it yourself. -- Colin -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
