unfortunately OurCacheListener is an interface and has 10-20 implementations and each one has totally different @Inject fields in it. (ie. we need it to be completely dynamic based on the class that gets provided)
Putting in the Injector into the Provider was a nice idea, but then I ran into a problem with AtUnit :( .... it completely controls the Injector and I have no access to it so I can't get the Injector back down into my provider :(. I will have to check out guiceberry maybe, but at least it works outside of my unit tests now. thanks, Dean On Feb 18, 7:41 am, Sam Berlin <[email protected]> wrote: > If you have a customer provider, it should be easy to tell Guice to also > inject fields in what it provides. Something like > > MyProvider<Foo> implements Provider<Foo> { > MembersInjector<Foo> fooInjector; > @Inject MyProvider(MembersInjector<Foo> fooInjector) { > this.fooInjector = fooInjector; > } > Foo get() { > Foo foo = new Foo(); > fooInjector.inject(foo); > return foo; > } > > } > > or, a little more succinctly in an @Provides: > > @Provides Foo gimmeFoo(MembersInjector<Foo> fooInjector) { > Foo foo = new Foo(); > fooInjector.inject(foo); > return foo; > } > > MembersInjectors are built into Guice, so it can give you a type-specific > Injector for whatever object you need. It's a safer than requesting the > whole Injector because it narrows your dependencies. > > sam > > On Fri, Feb 18, 2011 at 9:07 AM, deanhiller <[email protected]>wrote: > > > Last night, I tried doing creating a CustomProvider BUT then Guice is > > not injecting my CustomProvider with fields that have @Inject in my > > objects that get created(I was hoping Guice intercepted them and > > created them on-demand). Is there a way to make this work.... > > > Someone has > > @Inject > > private Provider<OurCacheListener> listenerProvider; > > > and that client that will call listenerProvider.get() only has one > > requiremnet. They must set the ThreadLocal var below first so that > > the provider knows what object to instantiate, I was hoping to get > > Guice to then inject the rest of the dependencies in those created > > beans. Is there a way to do this? > > > public class ListenerProvider implements Provider<OurCacheListener> { > > > private static ThreadLocal<Class<?>> clazzThreadLocal = > > new ThreadLocal<Class<?>>(); > > > @Override > > public OurCacheListener get() { > > Class<?> clazz = clazzThreadLocal.get(); > > try { > > return (OurCacheListener) clazz.newInstance(); > > } catch (InstantiationException e) { > > throw new RuntimeException("could not construct," + > > " see attached exception", e); > > } catch (IllegalAccessException e) { > > throw new RuntimeException("failed", e); > > } > > } > > > public static void setType(Class<?> clazz2) { > > clazzThreadLocal.set(clazz2); > > } > > > } > > > On Feb 17, 10:11 pm, deanhiller <[email protected]> wrote: > > > We have a platform on top of NoSql environment where we have an > > > annotation like so on some beans > > > > @CacheListenerImpl(Loader.class) > > > or another entity has > > > @CacheListenerImpl(Processer.class) > > > > Now the hard part is I don't want to have to code up one line of these > > > for each of those lines > > > @Inject > > > private Provider<Loader> loader; > > > > I would rather has something a bit more dynamic like this > > > > @Inject > > > private Provider<CacheListener> provider; > > > > where CacheListener is an interface that Loader, Processor and a whole > > > slew of beans implement. Is there a way to do this at all with Guice > > > or is this a bit toooooo dynamic for Guice to handle? > > > > OR is there another way in guice more like this perhaps.... > > > > public CacheListener getList(Class c) { > > > return someGuiceProvider.create(c); > > > > } > > > > Basically, I want all the created classes to be able to use Guice > > > injection as well. > > > > thanks, > > > Dean > > > -- > > 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.
