You would not need to create multiple injectors, a single injector is sufficient. As you obtain an instance from the injector, all injections within the original object you obtain will be injected as well, in this way the injections cascade until the entire object graph is built.
On Jul 23, 11:30 am, Asier <[email protected]> wrote: > Hi all > > I'm new to Guice. We have one small application that runs in batch > mode; it's a console application that is launched via main() method. > And here comes the questions > > 1. Bootstraping > > I've seen that I need an Injector to get injected classes, but how > coud I achieve this? If I create an injector in the main() method it > isn't available to other classes, I mean, I must create Injectors in > every class I need it? > > With Spring or in a web environment the injector is accesible from > almost everywhere, but with console or batch applications it's very > easy to fall in creating some kind of factory class with a static > injector in with all modules registered. > > Is there a better approach? > > 2. Replacing factories > > I think I'm missing some point here. I can't see how can I get rid of > typical factories with Guice: I can't see how replace factories based > on input parameters :-? > > public class ServiceFactory { > public static IService getServiceByKey(String key) throws > Exception { > for (ServiceKey k: ServiceKey.values()) { > if (key.equalsIgnoreCase(k.getKey())) { > Class<?> klass = Class.forName(k.getKlass()); > return (IService) klass.newInstance(); > } > } > throw new Exception(); > } > > } > > interface IService { ... } > > class A implements IService { ... } > class B implements IService { ... } > > enum ServiceKey { > ONE ("one", "org.testorg.A"), > TWO ("two", "org.testorg.B"); > > private final String key; > private final String klass; > ServiceKey(String key, String klass) { > this.key = key; > this.klass = klass; > } > public String getKey() { return key; } > public String getKlass() { return klass; } > > } > > Perhaps this questions sound like "too newbie" but I'm beggining with > Guice and D-I. > > Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
