如何在Play Framework中使用Guice从配置加载注射器

时间:2018-11-16 11:28:20

标签: scala playframework guice

我在游戏框架中使用Guice,我通过配置为每个控制器加载依赖项,我有两个控制器ControllerA和COntrollerB 我需要在controllerB中使用ControllerA的实例

class ResourceADependencyModule extends AbstractModule { 
//bindings goes here
}

class ResourceBDependencyModule extends AbstractModule { 
//bindings goes here
}

在application.conf

play.modules.enabled += "models.guice.ResourceADependencyModule"
play.modules.enabled += "models.guice.ResourceBDependencyModule"

这是代码

class ControllerA @inject(resourcesA:ResourceA) extends Contoller {

}
class COntrollerB @inject(resourcesB:ResourceB) extends Contoller {

// i need to call some method of controllerA here for that i am doing it like this 

val injector = Guice.createInjector(new ResourceADependencyModule())
  val controllerB = injector.getInstance(classOf[ControllerA])
  def someTask = controllerB.methodOfCOntrollerA 

}

}

在这里,我想更改要从conf文件注册的实现(例如我可以读取models.guice.ResourceADependencyModule),而不要使用Guice.createInjector的方式加载ControllerA实例 有什么办法吗?

0 个答案:

没有答案