Telling Guice what Instance to use

4 views
Skip to first unread message

nino martinez wael

unread,
Nov 24, 2009, 5:30:42 AM11/24/09
to google...@googlegroups.com
Hi

A couple days ago I wrote to the forum, but I don't think my mail came through.

I have a case where only the code that needs injection can provide one of the objects that needs to be injected :(

The code looks something like this:

Myclass

public myclass(SpecialObject specialObject )
{
--->
InjectorCreator.InjectMembers(this);
}

@inject
public void setController(Controller controller){

}

And lastly the controller class:

Controller{

@inject
public void setSpecialObject(SpecialObject specialObject ){

}

.. @inject a lot of other stuff too

}

So my question are can I somehow make Guice aware of the SpecialObject instance at the ---> marker?

Further the instance reference has to be requestscoped or at least only use the specified instance for the current request only, myclass will only be instantiated once per request .

Regards Nino

Anton Gavazuk

unread,
Nov 24, 2009, 6:07:56 AM11/24/09
to google...@googlegroups.com
you can use a Provider and it will take care creating/getting required instance for injection.

2009/11/24 nino martinez wael <nino.mart...@gmail.com>

--

You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

nino martinez wael

unread,
Nov 24, 2009, 7:29:12 AM11/24/09
to google...@googlegroups.com
I know about providers and we are using them alot.

However I fail to see how I can tell a provider to use a specific instance only for that request. Should the pseudo code be something like this:


public myclass(SpecialObject specialObject )
{
--->provider.setInstance(SpecialObject);
InjectorCreator.InjectMembers(
this);
}

How would I get the provider? And how would I makes sure it were only in the current request so we don't get mixed instances over requests..?

regards Nino



2009/11/24 Anton Gavazuk <antong...@gmail.com>

nino martinez wael

unread,
Nov 24, 2009, 9:37:28 AM11/24/09
to google...@googlegroups.com
Ohh and forgot to mention that it is guice 1.0

Brian Pontarelli

unread,
Nov 24, 2009, 11:31:57 AM11/24/09
to google...@googlegroups.com
I'd assume something else is instantiating "myclass" and you need to inject it by hand. In this case, you have a couple of options:

- ThreadLocal
- Singleton/static container

I don't think Providers are scoped (I could be wrong on that) and therefore you can't get the Provider from the injector and then set the value into the provider. Using a ThreadLocal could work if it is safe to assume you are in a single thread. Otherwise, you might have to do some fancy foot work with a singleton or static container to hold the SpecialObject until the Provider can grab it.

-bp

nino martinez wael

unread,
Nov 25, 2009, 5:22:04 AM11/25/09
to google...@googlegroups.com
You assumed correct.

Could you show some pseudo code? Im a bit unsure of how I should use threadlocal.. You can scope a provider, providing you have a scope for it. I don't think a static container would work since, since instances of SpecialObject are request specific..

Thanks for your help so far, interesting point with threadlocal.

regards Nino

2009/11/24 Brian Pontarelli <br...@pontarelli.com>

Brian Pontarelli

unread,
Nov 25, 2009, 10:41:48 AM11/25/09
to google...@googlegroups.com
Code:

public class SpecialObjectHolder {
  public static ThreadLocal< SpecialObject > holder = new ThreadLocal< SpecialObject >();
}

public class MyProvider implements Provider<SomeOtherClass> {
  public SomeOtherClass get() {
    SpecialObject obj = SpecialObjectHolder.holder.get();
    return new SomeOtherClass(obj);
  }
}

public class MyClass {
  @Inject public SomeOtherClass otherClass;

  public MyClass(SpecialObject obj) {
    SpecialObjectHolder.holder.set(obj);
    InjectorCreator.getInjector().injectMembers(this);
  }
}

public class MyModule extends AbstractModule {
  public void bind() {
    bind(SomeOtherClass.class).toProvider(MyProvider.class);
  }
}


That should be roughly what you are looking for.

-bp

nino martinez wael

unread,
Nov 26, 2009, 7:01:11 AM11/26/09
to google...@googlegroups.com
thanks

2009/11/25 Brian Pontarelli <br...@pontarelli.com>
Reply all
Reply to author
Forward
0 new messages