Pergunta

We have a new Play 2.0 project and we are planning to introduce DI as we add some complex 3rd party integration code.

There is a Guice plugin for Play 2.0 but it looks like it will be obsolete at 2.1 and I have a hunch that 2.1 is not that far anymore.

https://github.com/typesafehub/play-plugins/tree/master/guice

Is Guice a safe bet for Play 2.0/2.1 or should we consider other options?

Foi útil?

Solução

I'd go with the 2.1 version, and the new controller instanciation from the Global object.

Here is a Guice example from the doc:

  import play.GlobalSettings;

  import com.google.inject.Guice;
  import com.google.inject.Injector;

  public class Global extends GlobalSettings {

    private static final Injector INJECTOR = createInjector(); 

    @Override
    public <A> A getControllerInstance(Class<A> controllerClass) throws Exception {
      return INJECTOR.getInstance(controllerClass);
    }

    private static Injector createInjector() {
      return Guice.createInjector();
    }

  }

You have to declare a special route for these controllers, with the special @:

GET    /myUrl       @controllers.MyController.myMethod()

And you can also take a look at this demo using Spring: https://github.com/guillaumebort/play20-spring-demo

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top