2010/1/26 eric <[email protected]> > OK, here the deal : > > I use JFormDesigner (a visual builder) , which generates an XML file, > that "declares" the tree of visual objects (JLabel, JTable, and, in > fact any kind of Beans (i.e. that can be instanciated with a > "newInstance" ) > > I feel that it was smart to "inject" those beans into my controller > class. > There is a runtime code, that reads the xml file, do the > instanciations , and provides a nice API based on the beans name. > > I've added an annotation @Widget for fields in my controllers , and I > use the name of the annotated field to inject the corresponding bean > instance. > > This is my "OwnInjector". It can't be replaced by guice, a simple > facade would be: > > class OwnInjector { > injectInto(Object o) ; > } > > and I've made a simple type listener > > > > > Now, as I've started using guice, and my controllers, my models are > injected by guice. > > I've added a TypeListener so that whenever a controller is injected by > guice, It is also by my OwnInjector. It works, cool. > > > class JFormDesignerInjector<T> implements MembersInjector<T> { > OwnInjector myOwnInjector = new MyOwnInjector(); > public void injectMembers(T t) { > if (provider != null) { > LOG.info("injecting guice/jformdesigner: " > + t.getClass()); > myOwnInjector.injectInto(t); > } > } > } > > > But now, I want to go one step further, I want to replace Bean > instanciation ( calling "newInstance" ) by guice instanciation in my > OwnInjector. Have changed the code that says : > > return super.newComponentInstance(beanClass, compName); > > by the new one > return guiceInjector.getInstance(beanClass); >
the TypeEncounter passed into your TypeListener has methods to lookup Providers: getProvider(Class<T> type) getProvider(Key<T> key) but you must be careful to only call the Provider when the injector has been created (for example in the MembersInjector or InjectionListener instance which you register with the type encounter, as they are only called once injection has begun) for a concrete example of how this works take a look at: http://www.sonatype.com/people/2010/01/from-plexus-to-guice-3-creating-a-guice-bean-extension-layer/ the previous article describes the general custom-injector approach: http://www.sonatype.com/people/2010/01/from-plexus-to-guice-2-the-guiceplexus-bridge-and-custom-bean-injection/ HTH Therefore the idea was to simply have my "ownInjector" injected too, > so it can get access to the instance "guiceInjector". > > But the problem is that I can't. And I feel stuck. > > I've been looking for a way to change my facade into : > class OwnInjector { > injectInto(Injector guiceInjector, Object o) ; > } > > and my inject by : > public void injectMembers(T t) { > if (provider != null) { > LOG.info("injecting guice/jformdesigner: " > + t.getClass()); > Injector guiceInjector = > Guice.getInjectorFor( t ); > myOwnInjector.injectInto(guiceInjector , t); > } > } > > > but, I can't find a way to retrieve the injector neither. > > > Thank for your help ! > > > > > On Jan 25, 3:48 pm, Fred Faber <[email protected]> wrote: > > I'm a touch confused, but maybe that's irrelevant. > > > > If you're creating the target instances through guice, you can inject > your > > dependencies directly into them. I suspect you know this, and that there > is > > another reason you're using member injection. > > > > If these instances are not being created through guice, you can request > > injection onto them using Binder::requestInjection() > > (http://google-guice.googlecode.com/svn/trunk/javadoc/index.html?com/g. > .. > > ) > > > > If neither of these solutions work, could you please work backwards and > > elaborate on the lifecycle of the target instances onto which you need to > > inject dependencies? > > > > > > > > On Mon, Jan 25, 2010 at 9:13 AM, eric <[email protected]> wrote: > > > Thank you for the answer, but it seems to me that your solution will > > > not work as required... > > > > > I want to "inject" stuff into my classes, but I don't want to call an > > > "injectoStuffInto(this)" for everyone and each class in my project, > > > this is simply NOT injection. > > > > > My classes that needs such injection are already annotated with my own > > > tag "@InjectMyStuff", therefore I already know I have to > > > "injectStuff" in it, and it works already, using TypeListener. > > > > > But the injectfStuff function use "Class.newInstance" (the javabean > > > standard) method to allocate "stuff". I just want to move to > > > injector.getInstance() instead, but I can't get my hands on a decent > > > injector while in the TypeListener (or anywhere else). > > > > > maybe with spi ? > > > > > On Jan 25, 1:58 pm, Fred Faber <[email protected]> wrote: > > > > inject Injector, not MyOwnInjector. > > > > > > On Jan 25, 2010 7:46 AM, "eric" <[email protected]> wrote: > > > > > > Hi, > > > > > > I've got a problem that I can't solve by myself with guice, a little > > > > help would be nice, thanks. > > > > > > I've got a routine, that injects some "stuff" from a resource into a > > > > given instance. Until now, the stuff that was injected was > > > > instanciated using "newInstance". That was nice, but now I'm getting > a > > > > guice-addict, I would like to instanciate this "stuff" using > > > > injector.getInstance(). > > > > > > 1/ solution could be : > > > > > > Use a TypeListener, > > > > > > public void injectMembers(T t) { > > > > LOG.info("injecting > guice/jformdesigner: " > > > + > > > > t.getClass()); > > > > myOwnInjector.injectInto(t); > > > > } > > > > } > > > > > > but myOwnInjector should have been instantiated by guice, And I can't > > > > make it !? > > > > > > 2/ a one liner in every class could go like this > > > > > > @Inject > > > > MyClass(MyOwnInjector in){ > > > > in.injectInto(this); > > > > > > } > > > > > > But, hey, that's a ugly dependency, isn't it ? > > > > > > Any ideas ? > > > > > > -- > > > > 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]<google-guice%[email protected]> > <google-guice%2bunsubscr...@google groups.com> > > > <google-guice%2bunsubscr...@google groups.com> > > > > . > > > > For more options, visit this group athttp:// > > > 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]<google-guice%[email protected]> > <google-guice%2bunsubscr...@google groups.com> > > > . > > > 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]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- Cheers, Stuart -- 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.
