简体   繁体   English

如何从 play 框架中的 application.conf 文件中读取属性?

[英]How to read a property from the application.conf file in play framework?

I am a bit new to play framework.我对玩框架有点陌生。 I am trying to get a property from the application.conf file to a controller class.我试图从 application.conf 文件中获取一个属性到一个控制器类。 Is there any way to do it?有什么办法吗?

Let's say we have defined a path as below in the application.conf假设我们在 application.conf 中定义了如下路径

ProxyPass /testPath / http://127.0.0.1:8080/

So I need to get it to a controller class.所以我需要把它放到一个控制器类中。 how can I do that?我怎样才能做到这一点?

Suppose you have added cache.aerospike.namespace=1234 in your application.conf , you can access it in your controller like this:假设您在application.conf添加了cache.aerospike.namespace=1234 ,您可以像这样在控制器中访问它:

public class MyController extends Controller {

   String nameSpace = Play.application().configuration().getString("cache.aerospike.namespace");
}

Just inject an instance of Config to the controller:只需向控制器注入一个Config实例:

import com.typesafe.config.Config;
import play.mvc.Controller;

import javax.inject.Inject;

public class MyController extends Controller {

    private final Config config;

    @Inject
    public MyController(Config config) {
        this.config = config;
    }
}

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

相关问题 如何在Glassfish上部署Play Framework 2.4应用程序并阅读application.conf - How to deploy play framework 2.4 application on glassfish and read application.conf Play Framework + Spring:从application.conf注入URL - Play Framework + Spring: Injecting URL from application.conf 如何在Play Framework(Java)2.3.x中的application.conf文件中为键设置值 - How to set a value to a key in application.conf file in Play Framework (Java) 2.3.x 在application.conf中包含的conf文件中使用Play Server的ID - Use ID of Play Server in a conf file included in application.conf 无法访问Play框架中的Cloud Foundry自动配置(application.conf文件) - Not able to access cloud foundry auto configuration in play framework (application.conf file) 在GlobalSettings onStart(Play Framework)中访问application.conf - Accessing application.conf in GlobalSettings onStart (Play Framework) Play Framework 2.7.0 Config,无需通过application.conf进行常规配置即可进行测试 - Play Framework 2.7.0 Config for tests without general configurations from application.conf 无法从Play框架2.4中的cron作业加载application.conf - Not able to load application.conf from cron job in play framework 2.4 从application.conf加载自定义conf文件时出错 - Error in Loading custom conf file from application.conf 使用 Play 从 java 类访问 application.conf 属性! 2.0 - Accessing the application.conf properties from java class with Play! 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM