Domanda

I just started learning how to use Guice and I'm having some trouble trying to configure assisted injections. I have the following interface:

public interface Individual extends Comparable<Individual>, Iterable<Long>{ ... }

It will be created by a factory. The constructor must receive a list of long:

public interface IndividualFactory {
    public Individual createIndividual(List<Long> chromossomes);
}

The implementation class has an @Assisted parameter to receive the list:

public class IndividualImpl implements Individual {
@Inject
public IndividualImpl(
    ConfigurationService configurationService,
    RandomService randomService,
    FitnessCalculatorService fitnessService,
    @Assisted List<Long> chromossomes
    ) { ... } 

Finally, this is my module class:

public class SimpleModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(Individual.class).to(IndividualImpl.class);
        install(new FactoryModuleBuilder().implement(Individual.class,
            IndividualImpl.class).build(IndividualFactory.class));
}

The problem is that this error is displayed when I run the project:

1) No implementation for java.util.ArrayList<java.lang.Long> annotated with @com.google.inject.assistedinject.Assisted(value=) was bound.
  while locating java.util.ArrayList<java.lang.Long> annotated with @com.google.inject.assistedinject.Assisted(value=)
    for parameter 3 at implementation.entities.IndividualImpl.<init>(IndividualImpl.java:25)
  at SimpleModule.configure(SimpleModule.java:36)

If I just remove the assisted parameter (not only the annotation but the parameter itself) everything works fine. I can't figure out what I'm doing wrong. I followed all the Guice tutorials I found and couldn't find an example of assisted parameter using a List<>; however even if I change this parameter to an Integer, for example, I get the same error.

È stato utile?

Soluzione

Remove:

bind(Individual.class).to(IndividualImpl.class);

With the binding you specified to use IndividualImpl for @Inject Individual. This makes no sense as you will NOT @Inject Individual anywhere in the code. You will @Inject IndividualFactory instead.

What would be remotely possible is

bind(Individual.class).toProvider(BlowUpWithUseFactoryExceptionProvider.class);

but it makes no sense to do so as the default beahavior is similar.

May I suggest:

Iterable<Long> -> DnaContaining
List<Long> -> DnaMaterial
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top