[英]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.