简体   繁体   English

Play Framework依赖注入

[英]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 我一直在寻找一些有关如何在Play Framework 2.1中使用Guice / Spring DI的有用信息

What I want to do is to Inject several Services in some DAO's and vice versa. 我想要做的是在一些DAO中注入几个服务,反之亦然。

Just need some clarification on this - With play 2.1, do you have to use an @ annotation within the routes file for DI? 只需要对此进行一些澄清 - 使用play 2.1,你是否必须在路径文件中使用@ annotation进行DI?

I've looked at this guide here - https://github.com/playframework/Play20/blob/master/documentation/manual/javaGuide/main/inject/JavaInjection.md 我在这里查看了这个指南 - 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. 并应用以下步骤在app中创建Global类并在Build.scala中添加GUICE依赖项,但在调用注入的对象时继续获取空指针异常。

Has anyone been able to get DI working in Play 2.1 using Guice? 有没有人能够使用Guice在Play 2.1中使用DI? I've seen examples across the internet but they all seem to be using DI within the controller. 我在互联网上看到了一些例子,但他们似乎都在控制器中使用DI。

I noticed you are using Java. 我注意到你正在使用Java。 Here is how I got it to work for injecting into a controller. 以下是我如何使用它来注入控制器。

First, I created the following 4 classes : 首先,我创建了以下4个类:

MyController: 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: MyInterface的:

package models;

public interface MyInterface {
    String foo();
}

MyImplementation2Inject: MyImplementation2Inject:

package models;

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

MyComponentModule: 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: 您可以通过将以下行添加到application.conf文件的末尾来执行此操作,该文件位于conf目录中:

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

I hope this was helpful to you. 我希望这对你有所帮助。 :) :)

I use cake pattern and my own version of Global overriding getControllerInstance 我使用蛋糕模式和我自己的Global覆盖getControllerInstance版本

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

Cheers 干杯

Ben

Sorry, this is a late response, but here's our example 对不起,这是一个迟到的回复,但这是我们的例子

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

Have you tried using some different approach to DI than Guice? 您是否尝试过使用与Guice不同的DI方法? We also tried implementing a project with Guice or Spring but ended in registering our dependencies in objects that implement trait such as: 我们还尝试用Guice或Spring实现一个项目,但最后在实现trait的对象中注册了我们的依赖项,例如:

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). 另一种可能适合你的好方法是蛋糕模式(只是google)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM