使用Play 2.5.x进行依赖注入问题

时间:2017-05-15 18:53:57

标签: scala dependency-injection playframework guice

我们的应用程序是使用 Play 2.5.10。构建的 在我们的一个自定义播放模块中,我们将一个类绑定到一个对象实例,如下所示

import play.api.inject.{Binding, Module}
import scalax.collection.io.json.descriptor.predefined.Di
import scalax.collection.io.json.descriptor.{Descriptor, StringNodeDescriptor}

    class CustomModule extends Module {
          override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
            Seq(
              bind[HierarchyGraph[String]]
                .to[HierarchyGraphImpl],
              bind[Descriptor[String]]
                .toInstance(new Descriptor(StringNodeDescriptor,Di.descriptor[String]()))
            )
          }
        }

我们的特征及其实现的类定义如下:

    trait HierarchyGraph[T] {
      def toJson(graph : Graph[T, DiEdge]) : JsValue
    }

    class HierarchyGraphImpl @Inject()
    (descriptor: Descriptor[String]) extends HierarchyGraph[String] {

      def toJson(graph : Graph[String, DiEdge]) : JsValue = {
        val jsText = JsonGraph(graph).toJson(descriptor)
        try {
          Json.parse(jsText)
        } catch {
          case e : Exception => Json.toJson(Json.obj())
        }
      }

   }

但是当我们运行应用程序时,我们会遇到以下运行时错误

CreationException: Unable to create injector, see the following errors:

1) Could not find a suitable constructor in scalax.collection.io.json.descriptor.Descriptor. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
  at scalax.collection.io.json.descriptor.Descriptor.class(Descriptor.scala:58)
  while locating scalax.collection.io.json.descriptor.Descriptor<java.lang.String>
    for parameter 0 at schema.authorization.HierarchyGraphImpl.<init>(HierarchyGraph.scala:27)
  at modules.authorization.AuthorizationModule.bindings(AuthorizationModule.scala:19):
Binding(interface schema.authorization.HierarchyGraph to ConstructionTarget(class schema.authorization.HierarchyGraphImpl)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)

1 error

有人可以帮助我们解决此错误。我们想知道我们的绑定到底出错了。

提前致谢

0 个答案:

没有答案