GuiceApplicationLoader configuration error

前端 未结 2 981
失恋的感觉
失恋的感觉 2021-01-26 00:01

So, I\'m trying to implement compile time DI with something that looks like this:

package modules

class MyModule extends AbstractModule {
  def configure() {
           


        
相关标签:
2条回答
  • 2021-01-26 00:15

    Use bindings instead of load:

    class MyApplicationLoader extends GuiceApplicationLoader {
      override protected def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
      initialBuilder
        .in(context.environment)
        .loadConfig(context.initialConfiguration)
        .overrides(overrides(context): _*)
        .bindings(new MyModule)
      }
    }
    
    0 讨论(0)
  • 2021-01-26 00:30

    I'm not sure what you are trying to achieve here, but that's not how compile time dependency injection works. Guice does its magic at runtime. However, if you want to have your dependencies ready as soon as the application starts, use eager loading. Guice already provides all the tools needed:

    MyModule

    class MyModule extends AbstractModule {
      def configure() {
        bind(classOf[MyT]).to(classOf[MyTImpl]).asEagerSingleton()
      }
    }
    

    application.conf

    play.modules.enabled += "modules.MyModule"
    

    Because MyTImpl will be loaded as a singleton, it must have no instance bound data. Think of an object in scala terms. Always the exact same instance of MyTImpl will be injected.

    0 讨论(0)
提交回复
热议问题