The difference is that when you inject the Injector, Foo can retrieve and use absolutely anything bound or JIT creatable in the Injector... you know nothing about what dependencies Foo may make use of by looking at what is injected in to it.
When you inject Provider<MyClass>, you know that you can only retrieve an instance of MyClass from it. Additionally, with Provider being an incredibly simple interface, it's as easy to test with as just injecting an instance of MyClass itself. Don't inject the Injector except in specialized situations that require it. Colin On Thu, Jun 24, 2010 at 7:25 AM, Nicolas ANTONIAZZI < [email protected]> wrote: > Hi, > > it might seems to be a stupid question, but what is exactly the > difference between : > > public class Foo { > @inject > private Injector injector; > > public void bar() { > MyClass c = injector.inject(MyClass.class); > } > } > > and : > > public class Foo { > @inject > private Provider<MyClass> myClassProvider; > > public void bar() { > MyClass c = myClassProvider.get(); > } > } > > > What is the best to use ? > > -- > 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]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- 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.
