In my Class, the client has this code well, you can get rid of the "static"s I realized later. and then the client does this to ask for the one it needs...
@Inject private ListenerProvider listenerProvider; and the client code then is... listenerProvider.setType(classFromSomeAnnotation); OurCacheListener listener = listenerProvider.get(); //returns an object of type classFromSomeAnnotation later, Dean On Feb 18, 11:39 am, Bob Lee <[email protected]> wrote: > On Fri, Feb 18, 2011 at 8: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.... > > How do you choose which implementation to ask for? I'd probably use a > binding annotation instead of setting a thread local. > > To make your code work: > > public class ListenerProvider implements Provider<OurCacheListener> { > > private static ThreadLocal<Class<?>> clazzThreadLocal = > new ThreadLocal<Class<?>>(); > > @Inject private Injector injector; > > @Override > public OurCacheListener get() { > * return (OurCacheListener) > injector.getInstance(clazzThreadLocal.get());* > } > > public static void setType(Class<?> clazz2) { > clazzThreadLocal.set(clazz2); > } > > } -- 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.
