Guice Modules for Play apps

As a former Java / Spring developer I liked the concept of configuration classes (i.e. Java classes annotated with the Spring @Configuration annotation).

I recently started to work with Play 2.3.x and Guice and one smell that I immediately found was the duplication in creating RedisClient objects.

Basically each Dao was reading the configuration and creating its own copy of the bean. While most of the RedisClient(s) were configured slightly differently, some Daos contained literally copy-and-paste code.

After some research I came across the Guice AbstractModule class and the @Provides annotation. For those of you familiar with Java + Spring a Guice Module is similar to a Java class annotated with the Spring @Configuration.

Below there is an example of the MyRedisService that relies on two RedisClients. The two RedisClients are configured in the Config Guice Moducle and individuated by their names.

The Config Module:

package config
import com.example.driver.RedisClient
import{AbstractModule, Provides}
class Config extends AbstractModule {
override def configure(): Unit = {
// Just make compiler happy
def dbOneRedisClient(actorSystem: ActorSystem): RedisClient = {
new RedisClient("localhost", 1234)(actorSystem)
def dbTwoRedisClient(actorSystem: ActorSystem): RedisClient = {
new RedisClient("localhost", 5678)(actorSystem)

view raw


hosted with ❤ by GitHub

The Service:

package services
import com.example.driver.RedisClient
class MyRedisService @Inject()(@Named("myDbOne") redisClientOne: RedisClient, @Named("myDbTwo") redisClientTwo: RedisClient) {
def doSomething = {

In order to link everything together and inform Guice of the new Config Module, it’s necessary to add:

play.modules.enabled += "config.Config"

in the configuration.

Full example and app available in:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: