On Fri, Jan 1, 2010 at 12:21 PM, JN <[email protected]> wrote:

> On Dec 31, 2:58 pm, "Dhanji R. Prasanna" <[email protected]> wrote:
> > On Fri, Jan 1, 2010 at 11:47 AM, JN <[email protected]> wrote:
> > > I'm looking seriously into the possibility of using guice for our
> > > project.  However, I'm extremely reluctant to pepper our interfaces
> > > and implementation classes with guice injections (based on various
> > > principles and practical concerns, I don't find it a good idea).  I'd
> > > like to use guice without using annotations, although I suspect it's
> > > not possible.
> > > Skimming the documentation, it seems like most of the annotations can
> > > be obviated by doing more work in the module.  However, doing an
> > > injection (I'd prefer constructor injection) seems to *require* the
> > > @Inject annotation.  Is there any way around this?
> >
> > Yes, you can use @Provides methods on your module or
> bind().toConstructor()
> > without any annotations at all.
>
> Thanks for the quick reply.  Does that bind to a no-arg constructor,
> or how do you specify which constructor to bind to?  A pointer to docs
> or javadoc would be helpful.  I tried to answer that question for
> myself, but couldn't find toConstructor() in the javadoc.  I explored
> AbstractModule ->bind() -> to()... At this point, all I see is to(),
> toInstance(), toProvider().
>
>
toConstructor() takes an argument of type Constructor<T> so you can point it
to any constructor you have. You can write yourself a utility that does
something like:

bind(MyObject.class).toConstructor(onlyConstructorOf(MyObject.class));

where onlyConstructorOf is a method that returns the sole constructor in a
class or throws an error.

Dhanji.

--

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.


Reply via email to