使用PlayFramework和Guice时如何处理循环依赖?

时间:2017-07-24 20:55:24

标签: scala playframework guice

我使用PlayFramework 2.6与Scala和Guice,我的组件之间有以下设计:

一些#Split `data` into rows and then convert to vector temp = do.call(c, lapply(1:NROW(data), function(i) data[i,])) #Advance along `temp` and see if `unique` gives all elements of uniqueList #Extract the first index where that happens ind = which(sapply(1:length(temp), function(i) identical(unique(temp[1:i]), uniqueList)))[1] #Divide the ind by number of column to obtain row number myrow = ceiling(ind/NCOL(data)) myrow #[1] 6 identical(sort(unique(as.vector(data[1:myrow,]))), uniqueList) #[1] TRUE 类对象:

Person

我很少(@Singleton class Person1 @Inject() (personApiService: PersonApiService, personDBApiService: PersonDBApiService) extends Person { override def gerPersonInfo(person: Person): Future[PersonInfo] = { ... } } Person1 ...)。

Person2我需要一个人员实例列表,所以我这样做了:

创造了一个特质:

PersonService

现在我可以做到:

trait PeopleManager {

  def application: Application

  def peopleList = List(
    classOf[Person1],
    classOf[Person2],
    ...
  )

  lazy val peopleInstances: List[Person] = peopleList.map(personClass => application.injector.instanceOf(personClass))
}

问题是,在我的控制器中,我得到class PersonService @Inject() (val application: Application) extends PeopleManager { // now I can use here peopleInstances. } 作为依赖,我得到循环依赖。因此,在我的控制器中,我必须按照PlayFramework文档的建议使用PersonService

Provider

这解决了循环依赖问题,但我现在在测试中遇到问题,为class MyController @Inject() (cc: ControllerComponents, peopleService: Provider[PersonServcie]) extends AbstractController(cc) { ... } 创建一个模拟。

我觉得这里的设计可能会更好,不会让我得到循环依赖错误,有人有一个很好的建议吗?

0 个答案:

没有答案