路径参数:无效路径Scala Play Config文件

时间:2015-02-09 16:47:49

标签: scala playframework playframework-2.0 playframework-2.3

我在application.conf文件中收到解析异常。

这是我的application.conf

db.test.driver = org.postgresql.Driver 
db.test.user = "postgres"
db.test.password = "postgres"
db.test.url = "jdbc:postgresql://localhost:5432/gasguru"

这是我尝试用来从我的application.conf

中读取的代码
trait DbConfigWeb extends DbConfig { qualifier: DbQualifier =>

  def url: String = current.configuration.getString(qualifier + ".url").get
  println(url)
  def username: String = current.configuration.getString(qualifier + ".user").get
  def password: String = current.configuration.getString(qualifier + ".password").get
  def driver: String = current.configuration.getString(qualifier + ".driver").get
  override def database: DatabaseDef = JdbcBackend.Database.forURL(url, username, password, null, driver)

  override implicit val session = database createSession
}

trait DbQualifier {
  val qualifier: String
}

trait DbProductionQualifier extends DbQualifier {
  override val qualifier = "db.production"
}

trait DbTestQualifier extends DbQualifier {
  override val qualifier = "db.test"
}

这是我试图运行的测试用例:

class DbConfigWebUnitTest extends PlaySpec with OneAppPerSuite with BeforeAndAfterAll {


      "DbConfigWebTest" must {
        "have the same username as what is defined in application.conf" in {
          val dbManagementWeb = new DbConfigWeb with DbTestQualifier
          dbManagementWeb.username must be("postgres")
        }

        "have the same password as what is defined in application.conf" in {
          val dbManagementWeb = new DbConfigWeb with DbTestQualifier
          dbManagementWeb.username must be("postgres")
        }


        "have the qualifier db.test" in { 
          val dbManagementWeb = new  DbConfigWeb with DbTestQualifier
          dbManagementWeb.qualifier must be ("db.test")
        }
      }


    }

最后是错误消息:

[info] - must have the qualifier db.test *** FAILED ***
[info]   com.typesafe.config.ConfigException$BadPath: path parameter: Invalid path 'com.suredbits.web.db.DbConfigWebUnitTest$$anonfun$1$$anonfun$apply$mcV$sp$3$$anon$3@1e2cbe08.url': Token not allowed in path expression: '$' ('$' not followed by {, '$' not allowed after '$') (you can double-quote this token if you really want it here)
[info]   at com.typesafe.config.impl.Parser.parsePathExpression(Parser.java:1095)
[info]   at com.typesafe.config.impl.Parser.parsePath(Parser.java:1135)
[info]   at com.typesafe.config.impl.Path.newPath(Path.java:224)
[info]   at com.typesafe.config.impl.SimpleConfig.hasPath(SimpleConfig.java:80)
[info]   at play.api.Configuration.reportError(Configuration.scala:743)
[info]   at play.api.Configuration.readValue(Configuration.scala:132)
[info]   at play.api.Configuration.getString(Configuration.scala:151)
[info]   at com.suredbits.web.db.DbConfigWeb$class.url(DbConfigWebProduction.scala:14)
[info]   at com.suredbits.web.db.DbConfigWebUnitTest$$anonfun$1$$anonfun$apply$mcV$sp$3$$anon$3.url(DbConfigWebUnitTest.scala:27)
[info]   at com.suredbits.web.db.DbConfigWeb$class.$init$(DbConfigWebProduction.scala:15)
[info]   ...

1 个答案:

答案 0 :(得分:2)

您正在使用字符串连接对象,该字符串将调用生成your.class.name@hash的默认toString方法。您需要从特征中调用方法:

current.configuration.getString(qualifier.qualifier + ".url").get

并在其他电话上也​​这样做。

或者你可以覆盖toString方法来返回限定符值,并以与现在使用它相同的方式使用它

trait DbQualifier {
  val qualifier: String
  override def toString = qualifier
}