can I create a provider that will return a new object each time get is called

1,024 views
Skip to first unread message

ohad shai

unread,
Mar 27, 2012, 8:35:37 AM3/27/12
to google-guice
I want to have a provider member in class, that when 'get' called will
give me a new object each time. how can I do that?

public class GuiceInjectionTest
{
@Inject
Provider<MyClass> provider;

public Provider<MyClass> get()
{
//I want a new instance every time here that is injected by
guice
return provider.get()
}
}

Note I want this new instance to be injected by Guice, so I cant just
create a `new MyClass()` in the provider implementation.

Willi Schönborn

unread,
Mar 27, 2012, 8:57:37 AM3/27/12
to google...@googlegroups.com
If the provider is not bound in any scope, this should be the default
behaviour. I don't quite get the meaning of the test though.

ohad shai

unread,
Mar 27, 2012, 9:02:18 AM3/27/12
to google-guice
I asked also in SO and found out how to do it:
http://stackoverflow.com/questions/9889561/can-i-create-a-provider-that-will-return-a-new-object-each-time-get-is-called

thanks anyway.

On Mar 27, 2:57 pm, "Willi Schönborn" <w.schoenb...@googlemail.com>
wrote:

Colin Decker

unread,
Mar 27, 2012, 9:49:32 AM3/27/12
to google...@googlegroups.com
I posted an answer to your question on SO. Note that the first answer you got there isn't the preferred way of doing this... you should inject a Provider as in your original example.

-- 
Colin


--
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.


ohad shai

unread,
Mar 27, 2012, 1:35:08 PM3/27/12
to google-guice
thanks!

On Mar 27, 3:49 pm, Colin Decker <cgdec...@gmail.com> wrote:
> I posted an answer to your question on SO. Note that the first answer you
> got there isn't the preferred way of doing this... you should inject a
> Provider as in your original example.
>
> --
> Colin
>
>
>
>
>
>
>
> On Tue, Mar 27, 2012 at 9:02 AM, ohad shai <ohads...@gmail.com> wrote:
> > I asked also in SO and found out how to do it:
>
> >http://stackoverflow.com/questions/9889561/can-i-create-a-provider-th...
Reply all
Reply to author
Forward
0 new messages