Scala 注入对象列表以播放应用程序上下文

Scala 注入对象列表以播放应用程序上下文,scala,playframework,playframework-2.0,guice,playframework-2.2,Scala,Playframework,Playframework 2.0,Guice,Playframework 2.2,在我的项目中,我有很多动物对象,例如: 其中一些具有依赖项注入: class Monkey @Inject() (wsClient: WSClient, configuration: Configuration) extends Animal { ... } 有些则不是: class Giraffe extends Animal { ... } 在我的animalservice类中,我需要所有动物对象实例的列表 目前,我的服务正在以依赖项注入的方式获取人员列表: class

在我的项目中,我有很多动物对象,例如:

其中一些具有依赖项注入:

class Monkey @Inject() (wsClient: WSClient, configuration: Configuration) extends Animal {
    ...
}
有些则不是:

class Giraffe extends Animal {
    ...
}
在我的
animalservice
类中,我需要所有动物对象实例的列表

目前,我的服务正在以依赖项注入的方式获取人员列表:

class AnimalsService @Inject() (animals: List[Animal]) {
    // here I can use animals as my desire
}
然后我有一个绑定类来绑定它:

class Bindings extends AbstractModule {
  override def configure(): Unit = {
    bind(classOf[AnimalsService]).toProvider(classOf[AnimalServiceProvider])
  }
}

object Bindings {
  class AnimalServiceProvider @Inject () (giraffe: Giraffe, monkey: Monkey ...) extends Provider[AnimalsService] {
    override def get: AnimalsService = {
      new AnimalsService(List(giraffe,monkey...))
    }
  }
}
这很好用,但我更喜欢的是在应用程序加载时以某种方式将列表添加到我的应用程序上下文中,这样我就不需要这样做了

这个当前的解决方案还意味着每当我需要一个新的动物时,我都需要向
AnimalServiceProvider
构造函数添加新的动物,并在这里添加新的
animalservice(列表(长颈鹿,猴子…)

处理这种情况的最佳方式是什么


我想可能使用guice的
@Named
注释,但不确定这是否正确,或者如何以这种方式命名对象列表…

我将使用一个具有列表和添加定义的方法的单例bean

class AnimalsService @Inject() () {
    val animals: List[Animal]
    def addAnimal(animal: Animal) {
        ....
    }
    // here I can use animals as my desire
}
然后在每一个需要动物的模块中,都需要一个额外的带有


只是一个简单的想法,缺少一些细节,我回家后会尽力补充。
class MonkeyConfigurator @Inject() (animalsService: AnimalsService) extends Animal {
    animalsService.add(this) //Or anything
    // here I can use animals as my desire
}