Guice eager singleton in production mode

946 views
Skip to first unread message

Tomer Praizler

unread,
Jan 30, 2016, 10:54:57 AM1/30/16
to play-framework
Hey, 

I have configured my guice module to instantiate one of my classes as eager in production and lazy in dev mode, based on guice documentation.

So this is my code:

import com.google.inject.{Singleton, AbstractModule}
import com.mylib.{MonitorActor, Reporter}
import play.api.libs.concurrent.AkkaGuiceSupport

class MyModule extends AbstractModule with AkkaGuiceSupport {
def configure() = {
bindActor[MonitorActor]("monitor-actor")

bind(classOf[Reporter]).in(classOf[Singleton])
}
}

The thing is, it doesn't get instantiated in production!

Only when using .asEagerSingleton(), but this is not good for me, I don't want this code to be instantiated in dev mode.

Am I missing anything?

Christian Schmitt

unread,
Jan 30, 2016, 12:41:52 PM1/30/16
to play-framework
in(Singleton) means something different.
The only thing would be binding to eagerSIngleton and then check the environment.mode if you run the code inside the binded class depending on environment.mode == Mode.Dev

Tomer Praizler

unread,
Jan 30, 2016, 2:54:52 PM1/30/16
to play-fr...@googlegroups.com
Thats awesome..it worked for me!
Out of curiosity when should I use in(Singlton) ? 

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/9K2DNnZnDYU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/77d2844f-d63b-407c-8f93-077690728f29%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Christian Schmitt

unread,
Jan 30, 2016, 3:56:11 PM1/30/16
to play-framework
Currently if you annotate your object with @Singleton it is the same as .in(Singleton)
Here is more about scopes.

However "normally" in(Scope.Singleton) would bind eagerly on production. However the Scope Singleton is more for a heavyweight object which shouldn't be created everytime.

Tomer Praizler

unread,
Jan 31, 2016, 8:13:54 AM1/31/16
to play-framework
Awesome!
Reply all
Reply to author
Forward
0 new messages