如何在Play框架中的`conf / application.conf`中查找变量?

时间:2017-03-19 13:23:30

标签: playframework

简介

根据this documentation,应该可以在conf/application.conf内查找环境变量。

  

使用环境变量

     

您还可以参考您的环境变量   application.conf文件:

my.key = defaultvalue
my.key = ${?MY_KEY_ENV}
     

这里,覆盖字段my.key = $ {?MY_KEY_ENV}只是消失了   MY_KEY_ENV没有价值,但是如果你设置了一个环境   变量MY_KEY_ENV例如,它将被使用。

     

因为您可以从其他变量中引用变量,   确保不要将环境变量命名为   字段名称。

     

例如:

MY_TOKEN = foo
MY_TOKEN = ${?MY_TOKEN} # Will reference the previous line, NOT your environmentally set variable

尝试

搬运工-compose.test.yml

sut:
  depends_on:
    - db
  ...
  environment:
    VAR_A: db:3306

CONF / application.conf

db.default.url="jdbc:mysql://${?VAR_A}/app"

问题

由于某种原因,无法查找变量:${: Name or service not known

sut_1  | [info] application - Creating Pool for datasource 'default'
sut_1  | [info] application - Creating Pool for datasource 'default'
sut_1  | [info] - should send 404 on a bad request *** FAILED ***
sut_1  | [info]   play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
sut_1  | [info]   at play.api.Configuration$.configError(Configuration.scala:154)
sut_1  | [info]   at play.api.Configuration.reportError(Configuration.scala:806)
sut_1  | [info]   at 
...
sut_1  | [info]   Cause: java.net.UnknownHostException: ${: Name or service not known
sut_1  | [info]   at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
sut_1  | [info]   at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
sut_1  | [info]   at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
sut_1  | [info]   at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
sut_1  | [info]   at java.net.InetAddress.getAllByName(InetAddress.java:1192)
sut_1  | [info]   at java.net.InetAddress.getAllByName(InetAddress.java:1126)
sut_1  | [info]   at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:190)
sut_1  | [info]   at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
sut_1  | [info]   at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2232)
sut_1  | [info]   at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
sut_1  | [info]   ...

问题

如何在Play Framework的conf/application.conf中查找变量?

1 个答案:

答案 0 :(得分:3)

将变量排除在字符串外:

db.default.url="jdbc:mysql://"${?VAR_A}"/app"