unleashed
unleashed

Reputation: 925

Play Framework Dependency Injection

I've been looking all over Google to find some useful information on how to use Guice/Spring DI in Play Framework 2.1

What I want to do is to Inject several Services in some DAO's and vice versa.

Just need some clarification on this - With play 2.1, do you have to use an @ annotation within the routes file for DI?

I've looked at this guide here - https://github.com/playframework/Play20/blob/master/documentation/manual/javaGuide/main/inject/JavaInjection.md

and applied the following steps creating a Global class in app and adding the GUICE dependencies in Build.scala but keep on getting a null pointer exception when invoking on the injected object.

Has anyone been able to get DI working in Play 2.1 using Guice? I've seen examples across the internet but they all seem to be using DI within the controller.

Upvotes: 3

Views: 6908

Answers (4)

Jon Burgin
Jon Burgin

Reputation: 83

I noticed you are using Java. Here is how I got it to work for injecting into a controller.

First, I created the following 4 classes :

MyController:

package controllers;

import play.mvc.*;
import javax.inject.Inject;

public class MyController extends Controller {

@Inject
private MyInterface myInterface;
    public Result someActionMethodThatUsesMyInterface(){
        return ok(myInterface.foo());
    }
}

MyInterface:

package models;

public interface MyInterface {
    String foo();
}

MyImplementation2Inject:

package models;

public class MyImplementation2Inject implements MyInterface {
    public String foo() { 
        return "Hi mom!";
    }
}

MyComponentModule:

package modules;

import com.google.inject.AbstractModule;
import models.MyInterface;
import models.MyImplementation2Inject;

public class ComponentModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(MyInterface.class).
                to(MyImplementation2Inject.class);
    }
}

Now the final part, that took me a silly long time to figure out, was to register the module. You do this by adding the following line to the end of the application.conf file, which is located in the conf directory:

play.modules.enabled += "modules.MyComponentModule"

I hope this was helpful to you. :)

Upvotes: 7

Christopher Hunt
Christopher Hunt

Reputation: 2081

Sorry, this is a late response, but here's our example

https://github.com/typesafehub/play-guice

Upvotes: 2

BenjaminParker
BenjaminParker

Reputation: 746

I use cake pattern and my own version of Global overriding getControllerInstance

https://github.com/benjaminparker/play-inject

Cheers

Ben

Upvotes: 2

Gevatter Tod
Gevatter Tod

Reputation: 131

Have you tried using some different approach to DI than Guice? We also tried implementing a project with Guice or Spring but ended in registering our dependencies in objects that implement trait such as:

trait Registry {
   def userDao: UserDao
...

}

object Registry {
  var current: Registry = _
}

object Environnment {
 object Dev extends Registry {
  val userDao = ...
//implement your environment for develpment here
}
 object Test extends Registry {
  val userDao = ...
//implement your ennviroment for tests here e.g. with mock objects
}
}

Another good approach wich might fit for you is the cake pattern (just google for it).

Upvotes: 1

Related Questions