I've added the example to the documentation at: http://code.google.com/p/google-guice/wiki/LinkedBindings
Christian. On Nov 7, 2009, at 6:09 PM, Jon Vaughan wrote: > > Aha, nice one. That wasn't mentioned (well, there was no example > like that) on http://code.google.com/p/google-guice/wiki/Bindings), > which was why I was confused. > > Thanks for the speedy response > > J > > > On Nov 7, 11:01 pm, dg <[email protected]> wrote: >> just >> >> bind(MyClass.class); >> >> -d >> >> On Nov 7, 12:58 pm, Jon Vaughan <[email protected]> wrote: >> >> >> >>> Hi, >> >>> I'm new to Guice, so I apologise if this is a stupid question, or if >>> my I'm just plain wrong. I'm trying to retrofit it to a large >>> existing codebase. >> >>> My understanding is that if I have a Guice binding like: >> >>> bind(MyInterface.class).to(MyClass.class) >> >>> where MyClass implements MyInterface and has a constructor like: >> >>> @Inject >>> public myClass(AnotherClass somethingElse) >>> { >>> //..blah >> >>> } >> >>> then when Guice supplies an instance of MyClass, that instance will >>> have been injected with, in turn, an AnotherClass instance, >>> providing >>> that is also bound (hopefully I'm right so far?) >> >>> So now imagine that MyClass does NOT implement MyInterface >>> (classes in >>> my codebase only implement interfaces where there are multiple >>> implementations of that interface) . >> >>> Guice does not permit me to do this: >> >>> bind(MyClass.class).to.(MyClass.class) >> >>> And this is not the same thing: >> >>> bind(MyClass.class).toInstance(new MyClass()) >> >>> because that instance will not have been supplied with an >>> AnotherClass. >> >>> I could have some sort of bootstrap where I: >> >>> bind(AnotherClass.class).toInstance(new AnotherClass()); >> >>> and then >> >>> Injector injector = Guice.createInjector(getBootstrapModule()); >>> bind(MyClass.class).toInstance(injector.getInstance(MyClass.class)); >> >>> but then I'm stumped if I want to then inject both MyClass and >>> AnotherClass into something, unless I can live with multiple >>> instances >>> of AnotherClass. >> >>> I do not want to write interfaces for these classes just so I can >>> inject them; have I got something wrong here, or is there a way >>> around >>> this? >> >>> Many thanks if you are able to help >> >>> Jon > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~----------~----~----~----~------~----~------~--~---
