Can Guice inject Scala objects

前端 未结 2 1261
忘了有多久
忘了有多久 2021-01-01 13:22

In Scala, can I use Guice to inject Scala objects?

For example, can I inject into s in the following object?



        
2条回答
  •  生来不讨喜
    2021-01-01 13:38

    The above answer is correct, but if you don't want to use ScalaGuice Extensions, you can do the following:

    val injector = Guice.createInjector(new ScalaModule() {
        def configure() {
          bind[String].toInstance("foo")
        }
    
        @Provides
        def guiceSpecProvider: GuiceSpec.type = GuiceSpec
      })
    

提交回复
热议问题