Friday, July 6, 2012

Install Guice Module


  1. If using,  play 2.0.2, add  "com.typesafe" % "play-plugins-guice" % "2.0.3" to your build.scala file. For play 2.0.1, add  "com.typesafe" % "play-plugins-guice" % "2.0.2" to your build.scala file.
  2. Create a file called play.plugins in your app/conf directory
  3. Add 1500:com.typesafe.plugin.inject.GuicePlugin
  4. Re-run eclipsify command from the play prompt. Initially, I didn't know that I have to do this, and I was struggling for several hours.
  5. Then define Dependency class in module package and inject the service.  That's it

Dependencies.java

package module;
import com.google.inject.*;
import service.*;

public class Dependencies implements Module {
 public void configure(Binder binder) {
     binder.bind(Service.class).to(SomethingService.class);
  }  
}

Application.java


import javax.inject.*;
import service.*;
public class Application extends Controller {  
  @Inject static Service s;
  public static Result index() {
    return ok(index.render(s.demonstrate()));
  }  

No comments:

Post a Comment