Provider's aren't universal: a Provider<A> will only give you an A, it will never give you a B.
You binding is redundant: the "to" clause is unnecessary, and in fact the whole statement is not required in most cases. You just need to make your A variable a field of the servlet class instead of a local variable. It doesn't have to be public; Guice can inject private members. On Fri, Apr 29, 2011 at 3:55 PM, garey <[email protected]> wrote: > Thanks Christian. I'll try injecting a Provider since I don't always > want an A. > > To do this, must I have a binding? Since A does not have an > implementation, I put > > bind(A.class).to(A.class); > > in one of my modules, but I don't know if that is necessary or > correct, and it looks a bit silly. > > Garey > > On Apr 29, 3:13 pm, Christian <[email protected]> wrote: >> You can't inject variables that are local to a method body. You'll need to >> grab field references to A (or Provider<A>) during the servlet's >> construction. >> >> 2011/4/29 garey <[email protected]> >> >> >> >> >> >> >> >> > Hi - >> >> > I have a class that needs a database connection, so I have >> > provides method in a module and I have a constructor like this >> >> > class A { >> >> > @Inject >> > A(Connection c) { ...} >> >> > ... >> > } >> >> > now I have an init method in a servlet that I call if I want to >> > initialize an A. The method requires some parameters, so I do >> >> > public void init( <parameters>) { >> > @Inject A a; >> >> > ... >> > } >> >> > but I get a compile time error telling me that I cannot use @Inject in >> > this way. What fundamental concept am I not getting (this time)? >> >> > Thanks for any help; >> >> > Garey Mills >> >> > -- >> > 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. > > -- 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.
