Return Singleton Queue<T> in guice

84 views
Skip to first unread message

Sneh Tekriwal

unread,
Dec 25, 2015, 1:51:33 PM12/25/15
to google-guice

I am using Google Guice for dependency injection and I require a singleton Queue<T>.

Here is the code to return the queue.


bind
( new TypeLiteral<Queue<Entity>>() { } ).annotatedWith(OurBulletQueue.class).to( new TypeLiteral<ArrayDeque<Entity>>() { } ).asEagerSingleton();


But obviously it is not returning me a singleton due to some odd reason (My Guess is because I am binding it to new TypeLiteral). Any idea on how to fix it or is there any better way to do it?

Stephan Classen

unread,
Dec 25, 2015, 3:30:44 PM12/25/15
to google...@googlegroups.com
Can you also post a snippet where you inject the queue. Maybe the reason lies there.

Dirk Olmes

unread,
Dec 29, 2015, 12:55:58 PM12/29/15
to google...@googlegroups.com
On 12/25/2015 07:51 PM, Sneh Tekriwal wrote:
>
> <http://stackoverflow.com/questions/34464489/return-singleton-queuet-in-guice#>
>
> I am using Google Guice for dependency injection and I require a singleton
> Queue<T>.
>
> Here is the code to return the queue.
>
>
> bind(
> new TypeLiteral<Queue<Entity>>() { }
> ).annotatedWith(OurBulletQueue.class).to(
> new TypeLiteral<ArrayDeque<Entity>>() { }
> ).asEagerSingleton();

Does bind(...).annotatedWith(..).to(new
ArrayDequeue<Entity>()).in(Scopes.SINGLETON); work?

-dirk

Reply all
Reply to author
Forward
0 new messages