在Scala Generics中使用构造函数参数来填充类

时间:2015-02-13 13:38:35

标签: scala generics manifest

我有两个课我想概括,

object Server {
  final def main(args: Array[String]) {
    if (args.length < 1) {
      System.err.println("path to config folder required.")
      System.exit(-10)
    }
    run(args(0), false)
  }

  def run(configPath: String, parallel: Boolean): VendorIdentityManagerServer = {
    val config: AppConfig = Config.load(classOf[AppConfig], configPath)
    run(config, parallel)
  }

  def run(config: AppConfig, parallel: Boolean): VendorIdentityManagerServer = {
    val server = new VendorIdentityManagerServer(config)
    if (parallel) {
      new Thread() {
        override def run(): Unit = {
          server.main(Array("-admin.port", config.monitoringConfig.port))
        }
      }.start()
    } else {
      server.main(Array("-admin.port", config.monitoringConfig.port))
    }
    return server
  }
}

class VendorIdentityManagerServer(override val config: AppConfig) extends AbstractFinagleServerWithAdminInterface(config) {
  val injector = Guice.createInjector(new VendorIdentityManagerModule(config))
  val service = injector.getInstance(classOf[VendorIdentityManagerImpl])
  def main() {
    start(service)
  }
}

AppConfig的类型为FinagleServerConfig

我试过概括

class BasicFinagleServer[T <: AbstractFinagleServerWithAdminInterface, C <: FinagleServerConfig](implicit serverClassTag: ClassTag[T], configClassTag: ClassTag[C]) {
  def run(configPath: String, parallel: Boolean): T = {
    val config: C = Config.load(configClassTag.runtimeClass.asInstanceOf[Class[C]], configPath)
    run(config, parallel)
  }

  def run(config: C, parallel: Boolean): T = {
    val server = serverClassTag.runtimeClass.newInstance().asInstanceOf[T];
    if (parallel) {
      new Thread() {
        override def run(): Unit = {
          server.main(Array("-admin.port", config.monitoringConfig.port))
        }
      }.start()
    } else {
      server.main(Array("-admin.port", config.monitoringConfig.port))
    }
    return server
  }
}
object Server extends BasicFinagleServer[VendorIdentityManagerServer, AppConfig]{
  final def main(args: Array[String]) {
    if (args.length < 1) {
      System.err.println("path to config folder required.")
      System.exit(-10)
    }
    run(args(0), false)
  }
}

实例化服务器时,我得到NoSuchMethodError,因为VendorIdetityManagerServer期望将配置传递给它。

所以问题是如何将传递给run方法的配置传递给构造函数

0 个答案:

没有答案