如何在Play中管理不同环境的配置属性

Cyr*_* N. 2 playframework

在Play Framework中,我注意到可以分离Dev或Prod模式中使用的配置属性.

最好的使用示例是baseUrl:

# Url-resolving in Jobs
# ~~~~~~
# When rendering templates with reverse-url-resoling (@@{..}) in Jobs (which do not have an inbound Http.Request),
# ie if sending a HtmlMail, Play need to know which url your users use when accessing your app.
# %test.application.baseUrl=http://localhost:9000/

%dev.application.baseUrl=http://127.0.0.1:9000
%prod.application.baseUrl=http://www.example.com
Run Code Online (Sandbox Code Playgroud)

但我不能让它适用于另一个属性:

%dev.application.staticUrl=/public
%prod.application.staticUrl=http://static.example.com
Run Code Online (Sandbox Code Playgroud)

调用Play.configuration.getProperty("application.staticUrl"),甚至Play.configuration.getProperty("%dev.application.staticUrl")(测试)都没有:/

我怎样才能做到这一点?

emt*_*t14 5

在开发模式下运行时,您不需要在您的行前面添加dev.

我在使用2个实例开发app和prod模式时以开发模式运行我的应用程序.我的application.conf示例:


application.mode=dev
%inst1.application.mode=prod
%inst2.application.mode=prod
mail.smtp=mock
%inst1.mail.smtp=MAILSERVER1
%inst2.mail.smtp=MAILSERVER1

使用play run myapp运行应用程序将使用不带前缀的属性.在prod模式下,我运行2个实例,播放开始 - %inst1播放开始 - %inst2.

这将创建2个使用自己的属性运行的应用程序实例,如果未指定,则创建默认属性.

当您使用getProperty时,永远不要使用前缀,即Play.configuration.getProperty("mail.smtp")将在开发模式下返回模拟或在prod模式下返回MAILSERVER1.

在您的情况下,您有两种配置(不要误认为运行模式!),dev和prod.应用程序运行模式由application.mode属性定义.