dependency injection - Best way to inject a class whose instantiation cannot be controlled? -


I'm definitely new to Guice, so hopefully this is not a clear question ...

I am working with a framework that dynamically uses one of my application classes (we call it C ), so I have no control over its immediate is. Apart from this, there is no easy way for me to reach the newly created C , because the structure has tilted it, what would be the c like this:

 Public class C implies I {public C () {// This CTOR is enforced by the framework, hence DI can not be used? } Public Zero Start () {// This is an immediate hook, which will be F / W auto void) (Dependency = D Dependency .Just Instance (); d.doSmth ();}}  < P> I should have  dependence  to get the  C  using the DI. Here the  dependency  is clearly a singleton but this is not usually the case .  

I have come up with two possible solutions, neither is very clean, so I thought expert Ask me:

  1. Static injection I am struggling to see that this service significantly improves anti-locator patterns, however - I end up with last final static fields

  2. Support for a hook for C to initialize itself after the framework readiness (The initialize ( method shown above) This method is used for the setter injection. Using the newly created example can self-inject.

    With 2, I think that means external externality exceeds but at least the dependency is slightly More clearly shows things like unit testing are the trouble that I get my hands on a gadget injector in this case? (Not yet trusting another service locator) I have also read (and agree with this) which is clearly suspicious practice in the app code in the context of the injector.

    Your insights will really be appreciated.

    Many thanks, you can try injecting the investigators steadily. Static injection is more odd to test. But the providers have probably validly approved it to successfully validate their reliance on Guice.

      I apply the public class {@ Intensive static provider & lt; Dep1 & gt; Dep1Provider; @ Inject static provider & lt; Dep2 & gt; Dep2Provider; ... Public Zero Start () {Dep1 dep1 = dep1Provider.get (); Dep2 dep2 = dep2Provider.get (); ...}}    

Comments