Initialising Singleton on application start causes bean to be instantiated twice

11 views
Skip to first unread message

Kim Thrysøe

unread,
Jun 19, 2017, 3:37:58 AM6/19/17
to Play Framework
In an attempt to construct a @Singleton bean on application boot I followed the recommendation onhttps://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection (and in multiple threads in this group), ie. making a module in which the bean is explicitly bound and eagerly created with bind(...).asEagerSingleton()

This works as intended, instantiating the class. 

But the problem comes when the first request to the application also instantiates the class effectively creating a second singleton. 

I suspect that the singleton in the new Module may be created in a separate "Guice application context"? or are multiple class loaders in play? 

Can anyone point me the right direction for handling this situation of instantiating a @Singleton bean on application boot, in a way where that specific instance will be the one to later be injected into for example a Controller?

Thank you. 
Kim

Greg Methvin

unread,
Jun 19, 2017, 4:00:01 AM6/19/17
to play-framework
If you've bound the class as an eager singleton, it should be the same one later injected into the controller. Exactly one instance should be created by the injector, since it is a singleton.

Can you share a small sample of code that behaves like this?

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/af555749-d693-4fb4-89d2-40b8226da901%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Tech Lead - Play Framework

Reply all
Reply to author
Forward
0 new messages