Play 2.4 WebSocket在使用编译时依赖注入时抛出InstantiationException
Posted
技术标签:
【中文标题】Play 2.4 WebSocket在使用编译时依赖注入时抛出InstantiationException【英文标题】:Play 2.4 WebSocket throws InstantiationException when using compile-time dependency injection 【发布时间】:2015-07-10 09:13:23 【问题描述】:在使用编译时依赖注入时,Play 2.4 中的 WebSockets 似乎不起作用。我正在使用最新版本 2.4.2。具体来说,它在连接时会引发以下异常:
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[InstantiationException: null]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:254) ~[play_2.11-2.4.0.jar:2.4.0]
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:180) ~[play_2.11-2.4.0.jar:2.4.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$messageReceived$1.applyOrElse(PlayDefaultUpstreamHandler.scala:182) [play-netty-server_2.11-2.4.0.jar:2.4.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$messageReceived$1.applyOrElse(PlayDefaultUpstreamHandler.scala:180) [play-netty-server_2.11-2.4.0.jar:2.4.0]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.6.jar:na]
at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:215) [scala-library-2.11.6.jar:na]
at scala.util.Try$.apply(Try.scala:191) [scala-library-2.11.6.jar:na]
at scala.util.Failure.recover(Try.scala:215) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [scala-library-2.11.6.jar:na]
...
Caused by: java.lang.InstantiationException: null
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) ~[na:1.8.0_45]
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_45]
at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_45]
at play.api.inject.NewInstanceInjector$.instanceOf(Injector.scala:49) ~[play_2.11-2.4.0.jar:2.4.0]
at play.api.inject.SimpleInjector$$anonfun$instanceOf$1.apply(Injector.scala:85) ~[play_2.11-2.4.0.jar:2.4.0]
at scala.collection.MapLike$class.getOrElse(MapLike.scala:128) ~[scala-library-2.11.6.jar:na]
at scala.collection.AbstractMap.getOrElse(Map.scala:59) ~[scala-library-2.11.6.jar:na]
at play.api.inject.SimpleInjector.instanceOf(Injector.scala:85) ~[play_2.11-2.4.0.jar:2.4.0]
at play.api.inject.SimpleInjector.instanceOf(Injector.scala:80) ~[play_2.11-2.4.0.jar:2.4.0]
at play.api.Application$$anonfun$instanceCache$1.apply(Application.scala:234) ~[play_2.11-2.4.0.jar:2.4.0]
at play.api.Application$$anonfun$instanceCache$1.apply(Application.scala:234) ~[play_2.11-2.4.0.jar:2.4.0]
at play.utils.InlineCache.fresh(InlineCache.scala:69) ~[play_2.11-2.4.0.jar:2.4.0]
at play.utils.InlineCache.apply(InlineCache.scala:62) ~[play_2.11-2.4.0.jar:2.4.0]
at play.api.libs.concurrent.Akka$.system(Akka.scala:37) ~[play_2.11-2.4.0.jar:2.4.0]
...
控制器的外观如下:
class Application extends Controller
import play.api.Play.current
def socket = WebSocket.acceptWithActor[String, String] request => out =>
WebSocketActor.props(out)
object WebSocketActor
def props(out: ActorRef) = Props(new WebSocketActor(out))
class WebSocketActor(out: ActorRef) extends Actor
def receive =
case msg: String =>
out ! ("Received message: " + msg)
下面是ApplicationLoader
的样子:
class AppLoader extends ApplicationLoader
override def load(context: Context): Application = new AppComponents(context).application
class AppComponents(context: Context) extends BuiltInComponentsFromContext(context)
lazy val appController = new controllers.Application
lazy val router = new Routes(httpErrorHandler, appController, assets)
lazy val assets = new controllers.Assets(httpErrorHandler)
【问题讨论】:
【参考方案1】:原来这是 Play 2.4.2 中的一个错误。目前有一个pull request 来修复它。
在它修复之前,我通过覆盖AppComponents
的injector
来解决它,同时包括actorSystem
。所以AppComponents
变成了:
class AppComponents(context: Context) extends BuiltInComponentsFromContext(context)
lazy val appController = new controllers.Application
lazy val router = new Routes(httpErrorHandler, appController, assets)
lazy val assets = new controllers.Assets(httpErrorHandler)
// temporary workaround until issue #4614 in playframework is fixed. See https://github.com/playframework/playframework/issues/4614
override lazy val injector = new SimpleInjector(NewInstanceInjector) + router.asInstanceOf[play.api.routing.Router] + crypto + httpConfiguration + actorSystem
【讨论】:
以上是关于Play 2.4 WebSocket在使用编译时依赖注入时抛出InstantiationException的主要内容,如果未能解决你的问题,请参考以下文章
由于构建或路由导致 Play Framework 2.4 编译错误
将 Play 框架中的 WebSockets 从 2.4 版转换为 2.6 版
Play Framework 2.5,到 Web 套接字的路由无法编译
Play Framework - 是不是可以在 Play 中结合使用 Promise 和 WebSocket?