Play 2.5.3:使用依赖注入来获取配置值 [英] Play 2.5.3: Using dependency injection to get configuration values

查看:348
本文介绍了Play 2.5.3:使用依赖注入来获取配置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Playframework应用程序从2.4迁移到2.5.3并且我从 application.conf 文件中获取值时出现问题:

I'm trying to migrate a Playframework application from 2.4 to 2.5.3 and I have problems to get values from application.conf file:

之前从 application.conf获取值我的工作是:

Play.application().configuration().getString("label")

现在作为不推荐使用Play.application(),我应该使用依赖注入。根据框架文档,我使用以下说明:

Now as Play.application() is deprecated, I should use Dependency injection. Based on the framework documentation I use the following instructions:


  1. 定义导入:import javax.inject。*; import play.Configuration;

  2. 定义类属性 @Inject private配置配置;

  3. 在我班上使用配置类属性

  1. Define import: import javax.inject.*; import play.Configuration;
  2. Define class property: @Inject private Configuration configuration;
  3. Use the configuration class property on my class

当我在我的控制器上按照这些说明 Application.java 它完美运行时:

When I follow these instructions on my controller Application.java it is working perfectly:

但是当我尝试要在我的项目的其他类对象上使用它,依赖注入不起作用,我总是得到 NullPointerException

But when I try to use it on an other class object from my project, the dependency injection is not working and I always get a NullPointerException.

有人可以给我一个关于如何使用依赖注入从 application.conf 获取值的示例吗?

Can someone give me an example about how to get values from application.conf using dependency Injection?

我尝试使用DI的java代码的一部分:

Some part of my java code where I try to use the DI:

import javax.inject.Inject;
import play.Configuration;
import play.Logger;

public class Zipper {

    @Inject private  Configuration configuration;

    public void unZip(String zipFilePath) {
        Logger.debug("Display : zipFilePath"+zipFilePath);
        Logger.debug("before call parameter from application.conf");
        Logger.debug("configuration.getString = "+configuration.getString("Unzipedfile.path"));
        Logger.debug("aftercall parameter from application.conf");
    }
}

我总是得到一个空指针异常与一致的行configuration.getString(Unzipedfile.path)

And I always get a null pointer exception, at the line with configuration.getString("Unzipedfile.path")

推荐答案

我把答案放在这里,以帮助任何有同样问题的人

I put here the answer, in order to help anyone with the same issue

我的错误来自于我用来从我的调用类实例化Zipper java类的方式。

My error came from the way i used to instantiate my Zipper java class from my calling class.

向Igmar Palsenberg致谢,他向我提供了答案:
https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/play-framework/uLFqTM9_Iy4

Thx to Igmar Palsenberg, he provided me the answer : https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/play-framework/uLFqTM9_Iy4

我使用 Zipper zipTest = new Zipper(); 来实现我的Zipper类,我必须使用 Zipper zipTest = injector.instanceOf(Zipper.class);

I used Zipper zipTest = new Zipper(); to instanciate my Zipper class and i have to use Zipper zipTest = injector.instanceOf(Zipper.class);

这篇关于Play 2.5.3:使用依赖注入来获取配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆