scala play中的类强制转换异常

时间:2016-10-26 12:30:13

标签: scala akka playframework-2.5

我正在尝试使用play.api.Configuration对象来摆脱弃用警告

import com.datastax.driver.core.Cluster
import com.datastax.driver.core.policies.ConstantReconnectionPolicy
import play.api.Play.current
import play.libs.Akka

import scala.concurrent.ExecutionContext
import scala.collection.JavaConverters._
import play.api.Configuration
import javax.inject.Inject

object TestClient {

  val config = new Configuration(_)

 private val nodes = config.asInstanceOf[Configuration].getStringList("Test_ENDPOINT").map(_.asScala.toList).getOrElse(List("127.0.0.1"))

}

当我运行我的应用程序时,我收到错误

Caused by:  java.lang.ClassCastException: utils.TestClient$$anonfun$1 cannot be cast to play.api.Configuration

由于链依赖性,我无法将此对象转换为类。以下是完整的跟踪:

play.api.UnexpectedException: Unexpected exception[ExceptionInInitializerError: null]
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:184)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:131)
    at scala.Option.map(Option.scala:146)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:131)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:129)
    at scala.util.Success.flatMap(Try.scala:231)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:129)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:121)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
Caused by: java.lang.ExceptionInInitializerError: null
    at Global$.onStart(Global.scala:10)
    at play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:111)
    at play.api.Play$$anonfun$start$1.apply(Play.scala:102)
    at play.api.Play$$anonfun$start$1.apply(Play.scala:102)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.api.Play$.start(Play.scala:102)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:171)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:131)
    at scala.Option.map(Option.scala:146)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:131)
Caused by: java.lang.ClassCastException: utils.TestClient$$anonfun$1 cannot be cast to play.api.Configuration
    at utils.TestClient$.<init>(TestClient.scala:17)
    at utils.TestClient$.<clinit>(TestClient.scala)
    at Global$.onStart(Global.scala:10)
    at play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:111)
    at play.api.Play$$anonfun$start$1.apply(Play.scala:102)
    at play.api.Play$$anonfun$start$1.apply(Play.scala:102)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.api.Play$.start(Play.scala:102)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:171)

还尝试使用配置加载但无法正常工作。

val config = Configuration.load() 

此行有多个标记:

overloaded method value load with alternatives: (environment: play.api.Environment)play.api.Configuration <and> (environment: play.api.Environment,devSettings: Map[String,AnyRef])play.api.Configuration cannot be applied to ()
overloaded method value load with alternatives: (environment: play.api.Environment)play.api.Configuration <and> (environment: play.api.Environment,devSettings: Map[String,AnyRef])play.api.Configuration cannot be applied to ()

1 个答案:

答案 0 :(得分:0)

以下是您正在做的事情的说明:

scala> class Example(s:String)
defined class Example

scala> val  c = new Config(_) 
c: String => Example = <function1>

scala> c.asInstanceOf[Example]
java.lang.ClassCastException: $anonfun$1 cannot be cast to Example
  ... 42 elided

您正在尝试将函数Config => Configuration强制转换为配置。

查看Play's documentation,您将找到获取配置实例的多种替代方法。

Configuration.empty可能是一个开始。