Guice: is it possible to inject modules?

后端 未结 4 1247
Happy的楠姐
Happy的楠姐 2020-12-13 14:13

I have a Module that requires some Depedency. Is there a way Modules themselves can be injected? I realize this is a bit of a chicken and egg situation...

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 14:58

    What is the problem with initializing the module just by calling new MyModule(d) or by creating a Provider> that has an injected Injector? Those would appear to be the standard ways of handling this sort of problem. As has been mentioned, you can also use @Provides methods with arguments.

    If the dependency is optional then you can create the module and then call a setter to initialize the value if needed (e.g., com.google.inject.persist.jpa.JpaPersistModule does this with properties, while using new JpaPersistModule(String) to load the correct configuration).

    Otherwise I suppose it might be possible to do so (and then call createChildInjector(Modules... modules)), but I'd almost always prefer one of the other approaches to that one.

提交回复
热议问题