dependency injection - How to inject the default Guice Provider into my custom Provider? -
If I make a provider and bind it to a square, like
Bind (MyClass.class) .toProvider (MyClassProvider.class) then the provider < MyClass & gt; Automatically bind the to MyClassProvider . This is a problem if you use the provider & lt; MyClass & gt; If you want to inject MyClassProvider in , then this way: @Inject the public MyClassProvider (provider provider) Code> I want to inject the default provider into my provider so that I can easily create new examples within my custom provider. How to do this?
You will need to use one of two. If you do not comment on MyClass users, then it is quite easy. Public Class Audit {@Test Public Zero Test} {Injector Injector = Guice.createInjector (New Abstract Module) {@Override Protected Zero Configure () {Bind (MyClass.class). (Name name ("MYPROVIDER")). ToProvider (MyClassProvider.class);}}); MyClassUser user = Injector .get instance (MyClassUser.class); AssertTrue (user.get () myProvider.); // Show if I was created through my provider or Guice provider}} Category MyClassUser {Provider & lt; MyClass & gt; the provider; @com.google.inject.Inject Public MyClassUser (@MyPROVIDER) provider & lt; MyClass & gt; Provider) {this.provider = provider; } MyClass get () {return provider. Get (); }} Class MyClass {boolean myProvider = false; } Class MyClassProvider Provider Provider & lt; MyClass & gt; {Provider & lt; MyClass & gt; the provider; @com.google.inject.Inject Public MyClassProvider (provider provider) {this.provider = Provider; } @ Override Public Merge Mill () {MyClass c = provider.get (); C.myProvider = True; Return c; } Public string toasting () {return "Our provider"; }} If you want the MyClass users to be able to use this named method as the only way to use a named provider, then MyClass and MyClassProvider requires a " MyClass2 " provider as a solution to smell it, but has worked (instead of polluting this answer with bad response, Can find). You may be able to do this with a personal module or hair module (because this robot is a type of foot problem), but how am I unable to work it.
Comments
Post a Comment