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.

Reply via email to