简体   繁体   English

用 Java 玩 2.6 - 添加环境依赖注入

[英]Play 2.6 with Java - Adding Environment Dependency Injection

I need to use the Play.Environment in one of my java class as below.我需要在我的 java class 之一中使用 Play.Environment,如下所示。 I am using Play 2.6.x.我正在使用 Play 2.6.x。 Can you please help me in understanding, how to add dependency injection here.你能帮我理解一下,如何在这里添加依赖注入。 I tried using as below, but the env object is coming as NULL.我尝试如下使用,但环境 object 是 NULL。

MyFile.java MyFile.java

public final class MyFinalClass  {

 @Inject
 static Environment env;

public static String getFilePath(String fileName)  {
        return env.rootPath().getAbsolutePath();
}
static {
//It has a static block and some code here. I dont have any constructor.
}
}

Thanks谢谢

/***************************************************************************/ I have modified the above code and make it Singleton as below: /************************************************* ******************************/ 我已经修改了上面的代码,使其成为 Singleton 如下:

MyFinalClass.java
public class MyFinalClass   implements IConfigurationManager {

    @Inject Environment env;
    private static MyFinalClass   INSTANCE;

    //Pvt Constructor
    private MyFinalClass  () {

    }

    //Create Singleton Instance
    public static MyFinalClass instance() {
        if(INSTANCE == null) {
            INSTANCE = new MyFinalClass();
        }
        return INSTANCE;
    }
}

//Module.java
class Module extends AbstractModule {
    @Override 
    protected void configure( {

        bind(MyFinalClass.class).asEagerSingleton();
      }

Here I am getting Error as: Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.在这里我得到错误:类必须有一个(并且只有一个)用@Inject 注释的构造函数或一个非私有的零参数构造函数。

Then I modified as below:然后我修改如下:

Environment env;
//Pvt Constructor
@Inject private MyFinalClass  (Environment env) {
    this.env = env;
}

Here I am getting compilation issue the new MyFinalClass expects argument.在这里,我遇到了新的 MyFinalClass 期望参数的编译问题。 Can you please help in this.你能帮忙吗?

Thanks谢谢

Static field injection is not recommended . 不推荐Static 现场注入。 If you really need to have your class and method static you should use Guice's requestStaticInjection(..) .如果你真的需要你的 class 和方法 static 你应该使用 Guice 的requestStaticInjection(..) You could also make MyFinalClass a Singleton, inject Environment in the constructor and then inject MyFinalClass wherever you need to use getFilePath(..).您还可以将 MyFinalClass 设为 Singleton,在构造函数中注入 Environment,然后在需要使用 getFilePath(..) 的任何地方注入 MyFinalClass。

Edit : I see your comment about requestStaticInjection, so here is a short description about how to use it.编辑:我看到你对 requestStaticInjection 的评论,所以这里是关于如何使用它的简短描述。 Honestly I think it's more tidy to just make the class a Singleton.老实说,我认为将 class 设为 Singleton 会更整洁。 If you have stuff in MyFinalClass that you don't want to include in a Singleton maybe you should create a new class.如果您在 MyFinalClass 中有不想包含在 Singleton 中的东西,也许您应该创建一个新的 class。

To use requestStaticInjection() you have to do the request from a module, and this module needs to be loaded .要使用 requestStaticInjection() 你必须从一个模块发出请求,并且这个模块需要被加载

public class InjectorModule extends AbstractModule {
    @Override
    protected void configure() {
        requestStaticInjection(MyFinalClass.class);
    }
}

Edit 2 : You have changed your question now to use Singleton, but you are doing it wrong.编辑 2 :您现在已将问题更改为使用 Singleton,但您做错了。 Check the Play Docs , you just have to add the @Singleton annotation to your class.检查Play Docs ,您只需将 @Singleton 注释添加到您的 class 中。

Example where we create a singleton and a controller class that injects it (I'm writing this directly in the editor, not tested code but you get the idea):我们创建注入它的 singleton 和 controller class 的示例(我直接在编辑器中编写此代码,未测试代码,但您明白了):

@Singleton
public class MyFinalClass {
    private Environment env;

    @Inject
    public MyFinalClass(Environment env) {
        this.env = env;
    }

    public String getFilePath(String fileName) {
        return env.rootPath().getAbsolutePath();
    }
}

public class TestController extends Controller {
    @Inject
    private MyFinalClass myFinalClass;

    public Result index() {
        String fileName = "test.tst";
        String filePath = myFinalClass.getFilePath(fileName);

        return ok(filePath);
    }
}

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

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