Community contributed extensions

Guice module

The Guice module helps you to inject Guice managed components into your play application. The injection points are defined by the upcoming @javax.inject.Inject annotation, which is bundled with play at the moment.

There are two ways to use the module: you either can define your own injector (which can be useful if you have an existing injector or if you want to have full control over your modules) or play will scan the classpath for classes that implement com.google.inject.AbstractModule and inject based on these modules.

Enable the Guice module for the application

You can enable the Guice module by adding the following line to your application’s /conf/application.conf file:

module.guice=${play.path}/modules/guice

(if you are using a standard application.conf the line above should be already there, you will just need to uncomment it)

Defining a Guice injector

Defining a custom injector is easy, you just need to implement the play.modules.guice.GuiceSupport interface. For example:

package utils;
import play.modules.guice.GuiceSupport;
import com.google.inject.*;
public class GuicyDummy extends GuiceSupport {
        protected Injector configure() {
           return
           Guice.createInjector(
		//your modules should come here
           );
        }
}

After defining this class play will use the injector defined in the configure method to inject into your application’s Controller, Job and Mail classes (based on @javax.inject.Inject).

Using AbstractModules

Alternatively, play will scan the classpath for classes that implement AbstractModule.
For example:

package utils;
import com.google.inject.*;
public class GuicyDummy extends AbstractModule {
    public void configure() {
	//your bindings should come here
    }
}

In this case play will pick up this module and inject it into your application’s Controller, Job and Mail classes (marked by @javax.inject.Inject). Note, the custom injector strategy will always take precedent.

Inject classes other than Controller, Job and Mail

In order to inject classes that are neither controller nor Job and Mail, you need to annotate the class with play.modules.guice.InjectSupport annotation.
For example:

package models
import play.db.*
import play.modules.guice.*
import javax.inject.*
@InjectSupport
public class MyModel extends Model {
    @Inject
    static MyService service;
}

Limitations

The Guice module is not playing well with the Spring one, so please make sure you activate only one of them. It’s also worth mentioning that right now, following play’s convention, injection is working for static fields only.