Play.application()的替代方法是什么 [英] What is the alternative for Play.application()

查看:90
本文介绍了Play.application()的替代方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Play框架的新手.我想读取conf文件夹中的文件.所以我用

Play.application().classloader().getResources("Data.json").nextElement().getFile()

但是我知道那场比赛了.现在比赛已经过时了.我可以使用什么来读取文件. 我阅读了文章,几乎听不懂它在说什么.

I am new to Play framework. I wanted to read a file which is in the conf folder. So I used

Play.application().classloader().getResources("Data.json").nextElement().getFile()

But I got to know that play.Play is deprecated now. What can I use to read the file. I read this article and can hardly understand what it says.

推荐答案

只需将应用程序注入您需要的类中.假设它在控制器中:

Just inject the Application in the class where you need it. Supposing it is in a controller:

import play.Application;
import javax.inject.Inject;
import javax.inject.Provider;

class YourController extends Controller {

    @Inject
    Provider<Application> app;


    public Result someMethod() {
        // (...)
        // File is placed in conf/Data.json
        InputStrem is = app.get().classloader().getResourceAsStream("Data.json");
        String json = new BufferedReader(new InputStreamReader(is))
                .lines().collect(Collectors.joining("\n")); 
        return ok(json).as("application/json");    
    }
}

这篇关于Play.application()的替代方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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