I bind like this right now..
binder.bind(OurCacheListener.class).toProvider(ListenerProvider.class);
where ListenerProvider extends Provider<OurCacheListener>
AND that ONE provider provides 20 different kinds of classes. I found
out how to get the injector from AtUnit!!!!! so I am way further along
an inches from putting this to bed. I have the following in 2
locations....
@Inject
private ListenerProvider listenerProvider
How to make that a singleton as adding .in(Singleton) seems odd since
I don't want OurCahceListener to be a singleton and it didn't work
I also tried
binder.bind(ListenerProvider.class).in(Singleton.class); but that
didn't work.
I did figure AtUnit out...sweeeeet.....now I just need to figure out
how to make my Provider<OurCacheListener> into a singleton.
I now use the @ContainerClass in AtUnit like so.....(and I can come
back and get the injector...a bit of a kludge but it works now)....
public class BroadridgeGuiceContainer extends GuiceContainer {
private static Injector injector;
@Override
public Object createTest(Class<?> testClass, Map<Field, Object>
fieldValues)
throws Exception {
FieldModule fields = new FieldModule(fieldValues);
if (Module.class.isAssignableFrom(testClass)) {
injector = Guice.createInjector(fields,
(Module) testClass.newInstance());
} else {
injector = Guice.createInjector(fields);
}
return injector.getInstance(testClass);
}
public static Injector getInjector() {
return injector;
}
}
On Feb 18, 7:53 am, Sam Berlin <[email protected]> wrote:
> How are your providers being bound?
> sam
>
> On Fri, Feb 18, 2011 at 9:50 AM, deanhiller <[email protected]>wrote:
>
> > 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.
--
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.