Some years later, I had the same problem.
This appears to be fixed in Guice 3.0 as stated in
http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/assistedinject/FactoryModuleBuilder.html
.
However I need to use Guice 2.0 because of Resteasy compatibility.
Some flexibility in this matter would be nice.
Anyone found out a solution other than implementing the Factory?
On Saturday, February 14, 2009 5:01:23 AM UTC, Andy Davey wrote:
>
> Well I found a workaround.
>
> Basically instead of letting FactoryProvider create an implementation
> of SomeClassFactory, I created my own implementation:
>
> class SomeClassFactoryImpl implements SomeClassFactory
> {
> private SomeService m_service;
>
> @Inject
> public SomeClassFactoryImpl(SomeService service)
> {
> m_service = service;
> }
>
> public SomeClass createForKicks(Kicks kicks)
> {
> return new SomeClass(m_service, kicsk);
> }
>
> public SomeClass createForLaughs(Laughs laughs)
> {
> return new SomeClass(m_service, laughs);
> }
> }
>
> However, this seems like a backwards step to me. Why shouldn't a class
> be able to have multiple constructors that are annotated with @Inject?
>
> I understand that it's not to hard to come up with an example where
> Guice wouldn't know which constructor to call, because all required
> dependencies of every
> parameter for every constructor are available.
>
> But in the case of assisted injection, the factory method basically
> dictates which constructor to call.
>
> Does anyone else have any thoughts?
>
> On Feb 10, 9:39 pm, Andy Davey <[email protected]> wrote:
> > Hi,
> >
> > presently I have a class that has multiple constructors:
> >
> > class SomeClass
> > {
> > @AssistedInject
> > public SomeClass(SomeService service, @Assisted Kicks kicks)
> > {
> > ...
> > }
> >
> > @AssistedInject
> > public SomeClass(SomeService service, @Assisted Laughs laughs)
> > {
> > ...
> > }
> >
> > }
> >
> > Since I only want the SomeService parameter injected I've been using
> > the AssistedInject extension.
> >
> > Using the Assisted extension I've created a interface that looks
> > something like:
> >
> > interface SomeClassFactory
> > {
> > SomeClass forKicks(Kicks kicks);
> > SomeClass forLaughs(Laughs laughs);
> >
> > }
> >
> > And I bind it in the usual manner:
> >
> > bind(SomeClassFactory.class).toProvider(FactoryProvider.newFactory
> > (SomeClassFactory.class, SomeClass.class));
> >
> > Everything works fine _except_ @AssistedInject is now deprecated (I'm
> > currently using snapshot 20090205).
> >
> > The problem is, if I replace @AssistedInject with @Inject (which was
> > my understanding of what the documentation said to do) I get
> > exceptions trying to build the Injector:
> >
> > SomeClass has more than one constructor annotated with @Inject.
> > Classes must have either one (and only one) constructor annotated with
> > @Inject or a zero-argument constructor that is not private.
> >
> > My question is, is there a way forward that will allow me to have
> > multiple DI constructors that doesn't rely on deprecated code?
> >
> > Cheers,
> >
> > Andy
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-guice/-/xltf5J_-kocJ.
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.