播放依赖注入错误

时间:2016-06-01 23:32:49

标签: scala dependency-injection playframework-2.0

我正在尝试使用DI在我的应用程序中注入Web服务客户端。当我在被注入的对象中引用当前应用程序时,我得到运行时异常。下面是我的代码的骨架。

    **import play.api.Play.Current**
    @Singleton
    class MicorServiceClient@Inject()(ws: WSclient) {

     // References curret.configuraiton.getString(
    ....
    }


  class Application @Inject()(microServiceClient: MicorServiceClient) extends Controller { 

      def someMethod= Action {

      //Calls the micorservicelient.getResponse() 
      }
    }

当我的api被调用时,我看到以下错误。

@709lj5bmd: Unexpected exception
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:170)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:126)
    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:126)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:124)
    at scala.util.Success.flatMap(Try.scala:231)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:124)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:116)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
    at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1689)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error injecting constructor, java.lang.RuntimeException: There is no started application

1 个答案:

答案 0 :(得分:0)

啊,解决了这个问题,我需要将play.api.Configuration传递给客户端并删除对当前的引用

所以我的MicroServiceClient定义如下所示

class MicroServiceClient(ws: WSclient, configuration: play.api.Configuration) {
}