简体   繁体   English

在GlobalSettings onStart(Play Framework)中访问application.conf

[英]Accessing application.conf in GlobalSettings onStart (Play Framework)

I am using Java / Play Framework 2.1.0. 我正在使用Java / Play Framework 2.1.0。

I have found other questions asking how to access application.conf, and I tried two methods: 我发现了其他问题,询问如何访问application.conf,我尝试了两种方法:

Play.application().configuration().getString("my.thing");

and

ConfigFactory.load().getString("my.thing");

In both cases I dont get anything back. 在这两种情况下,我什么都收不到。 I am trying to do this in onStart of my Global class. 我正在尝试在Global类的onStart中执行此操作。 Maybe onStart happens before configuration is loaded? 也许onStart在加载配置之前发生?

How can I access values in application.conf from within onStart? 如何从onStart内访问application.conf中的值? (Or, to be more precise, how do I access values in myapp.conf which is included in application.conf) (或者,更确切地说,我如何访问application.conf中包含的myapp.conf中的值)

Can you verify that your onStart is getting called at all? 您可以验证您的onStart是否被调用吗?

The configuration values are loaded and accessible at that point. 此时将加载并访问配置值。 In fact, you are getting the play.Application instance passed as an argument, so you can just do this: 实际上,您正在获得play.Application实例作为参数传递,因此您可以执行以下操作:

  public void onStart(Application app)
  {
    super.onStart(app);
    String value = app.configuration().getString("my.thing");
    // do something with value
  }

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

相关问题 使用 Play 从 java 类访问 application.conf 属性! 2.0 - Accessing the application.conf properties from java class with Play! 2.0 如何从 play 框架中的 application.conf 文件中读取属性? - How to read a property from the application.conf file in play framework? Play Framework + Spring:从application.conf注入URL - Play Framework + Spring: Injecting URL from application.conf Play Framework 2.4 - 依赖注入以取代GlobalSettings.onStart() - Play Framework 2.4 - Dependency injection to replace GlobalSettings.onStart() Java Play 覆盖 application.conf 中的端口 - Java Play overwrites port in application.conf 在Play Java应用中标记化application.conf - tokenize application.conf in play java app 如何在Glassfish上部署Play Framework 2.4应用程序并阅读application.conf - How to deploy play framework 2.4 application on glassfish and read application.conf Play Framework 2.7.0 Config,无需通过application.conf进行常规配置即可进行测试 - Play Framework 2.7.0 Config for tests without general configurations from application.conf 无法访问Play框架中的Cloud Foundry自动配置(application.conf文件) - Not able to access cloud foundry auto configuration in play framework (application.conf file) 无法从Play框架2.4中的cron作业加载application.conf - Not able to load application.conf from cron job in play framework 2.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM