Ich Du, You do not need an instance of the injector, that's the magic on DI. Let's say you map a constructor annotated with @Inject and some of the objects that you inject also have inside them constructors annotated with @Injected and so on.
When your servlet is created the injector will take care to inject every mapped object in the annotated constructors / fields / setters recursively until the bottom of your object graph. Got it? Otherwise the whole loosed coupling would have gone once you would need to create a dependency of your injector and also from the class injected even not creating them using the new operator. This is similar to the factory pattern. *Jayr Motta* Software Developer * * I'm on BlackBeltFactory.com<http://www.blackbeltfactory.com/ui#!User/jmotta/ref=jmotta> ! On Fri, Jul 1, 2011 at 10:51 AM, ich du <[email protected]> wrote: > thx but as i mentioned in previous post, i already got a "MainModule" > called ServerModule. My Problem is how to use it on Serverside. All > Examples doing the bootstrapping in main - cretaeInjector, > getInstance. But in the guice/gwt/rpc case the injector instance is > created in GuiceServletContextListener. With this i can't get an > injector where i want it. in my special case my servlets need such an > injected class: > > public class MyServiceImpl extends RemoteServiceServlet implements > MyService { > > @Override > public ArrayList<SomeClass> getList() { > return injector.getInstance(Dataprovider.class).getList(); > } > } > > But how to get an Injector instance at this place? Should i create an > injector wherever i need one? or save the injector created inside my > GuiceServletContextListener and make it available via a getI()-Method? > > On Jul 1, 3:27 pm, jMotta <[email protected]> wrote: > > Ich Du, > > > > No, what you need is to create a module to "normal" injections, the one > that > > I've told you is a special one that deals with the special > characteristics > > of a servlet. > > > > The classes that will be inject must be also be mapped by Guice, you will > > need something like this: > > > > public class MainModule extends AbstractModule { > > @Override > > protected void configure() { > > bind(Objectify.class).toProvider(ObjectifyTransacionalProvider.class); > > > > > bind(Objectify.class).annotatedWith(Names.named("Non-Transactional")).toProvider(ObjectifyProvider.class); > > bind(ObjectifyFactory.class).toInstance(ObjectifyService.factory()); > > ... > > > > } > > } > > > > And then install this module with the servlet module that you've > installed > > before. I recommend you to read the guides in the Guice page, they're > very > > clear. :) > > > > *Jayr Motta* > > Software Developer > > * > > * > > I'm on BlackBeltFactory.com< > http://www.blackbeltfactory.com/ui#!User/jmotta/ref=jmotta> > > ! > > > > > > > > > > > > > > > > On Fri, Jul 1, 2011 at 10:08 AM, ich du <[email protected]> wrote: > > > thx again, > > > > > i just tried to wire up my server side with guice - the rpc part is > done so > > > far (thx to you). but on server side i also have some pojo > > > interfaces/implementation that i want to inject via guice. but how to > > > bootstrap this? at the moment i just added the module to my > > > GuiceServletContextListener: > > > > > public class AppGuiceContextListener extends > GuiceServletContextListener { > > > > > @Override > > > protected Injector getInjector() { > > > return Guice.createInjector(new ServerModule(), new > > > ServletModule()); > > > } > > > > > } > > > > > (not later than now i have to admit i am a total guice noob) > > > but imho somehow i need an injector instance on server side to get > > > instances of classes that use injection?! something like that: > > > Injector i = Guice.createInjector(new ServerModule(), new > > > ServletModule()); > > > ClassThatHasSom@InjectInside instance = i.getInstance( > > > [email protected]); > > > > > So on Server i need a reference to the injector instance?! is it a good > > > idea to store the injector in a (static?) field in > AppGuiceContextListener > > > or should i just call getInjector() whenever i need it? > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "google-guice" group. > > > To view this discussion on the web visit > > >https://groups.google.com/d/msg/google-guice/-/orv5N_ikTo4J. > > > > > 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. > > -- > 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. > > -- 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.
