Thanks Fred! I didn't know that when using Guice I can't create objects via new anymore.
Stefan On May 29, 10:07 pm, Fred Faber <[email protected]> wrote: > hello, > > The pattern you sketch would be more fitting for Guice / general DI if it > read: > > class A { > @Inject private SqlSession session; > > } > > class B { > private final A a; > > @Inject B(A a) { > this.a = a; > } > > } > > class C { > public static void main(String[] args) { > Injector injector = Guice.createInjector(...); > B b = injector.getInstance(B.class): > } > > } > > Meaning that the instance of A is created by Guice, and not by "new." > > In a case where you're stuck using "new" (e.g., when migrating code), you > can call injector.injectMembers(instance); > > Fred > > On Sat, May 28, 2011 at 9:52 PM, Stefan > <[email protected]>wrote: > > > > > > > > > Hi, > > is the following possible with Guice? > > > class A > > { > > @Inject > > private SqlSession session; > > } > > > class B > > { > > private A a; > > > public B() > > { > > a = new A(); > > } > > } > > > class C > > { > > public static void main() > > { > > ... > > B b = injector.getInstance(B.class); > > } > > } > > > I have a provider binded to class SqlSession. My problem is that > > session field in class A is not injected. Should I create injector in > > constructor of B and get instance of A from injector? Isn't it then > > easier to avoid Guice at all? I'm confused. > > > -- > > 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. -- 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.
