Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.0/2.1 for Java and dependency injection

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?

like image 945
Petteri H Avatar asked Nov 13 '12 09:11

Petteri H


1 Answers

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

like image 145
ndeverge Avatar answered Oct 07 '22 10:10

ndeverge