Ich bin neu bei Guice, also versuche ich AssistedInject zu verstehen. Ich habe sehr einfaches Projekt:Guice assistierte injizieren
Klasse, die ich injizieren will:
public class I1 {
}
Klasse mit unterstützter Injektion:
public interface ICla {
}
public class Cla implements ICla{
public Integer t;
public I1 i;
@Inject
public Cla(Integer t, @Assisted I1 i) {
this.t = t;
this.i = i;
}
}
Fabrik
public interface IClaFactory {
Cla create(Integer t);
}
und die Hauptklasse:
public class Main {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new Module());
IClaFactory factory = injector.getInstance(IClaFactory.class);
}
private static class Module extends AbstractModule {
protected void configure() {
install(new FactoryModuleBuilder()
.implement(ICla.class, Cla.class).build(IClaFactory.class));
}
}
}
Aber es funktioniert immer noch nicht und ich verstehe nicht, wo ich falsch liege?
Exception in thread "main" com.google.inject.CreationException: Unable to create injector, see the following errors:
1) No implementation for ru.test.factory.I1 annotated with @com.google.inject.assistedinject.Assisted(value=) was bound.
while locating ru.test.factory.I1 annotated with @com.google.inject.assistedinject.Assisted(value=)
for parameter 1 at ru.test.factory.Cla.<init>(Cla.java:11)
at ru.test.factory.IClaFactory.create(IClaFactory.java:1)
at com.google.inject.assistedinject.FactoryProvider2.initialize(FactoryProvider2.java:660)
at com.google.inject.assistedinject.FactoryModuleBuilder$1.configure(FactoryModuleBuilder.java:335) (via modules: ru.test.Main$Module -> com.google.inject.assistedinject.FactoryModuleBuilder$1)
2) Could not find a suitable constructor in java.lang.Integer. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at java.lang.Integer.class(Integer.java:52)
while locating java.lang.Integer
for parameter 0 at ru.test.factory.Cla.<init>(Cla.java:11)
at ru.test.factory.IClaFactory.create(IClaFactory.java:1)
at com.google.inject.assistedinject.FactoryProvider2.initialize(FactoryProvider2.java:660)
at com.google.inject.assistedinject.FactoryModuleBuilder$1.configure(FactoryModuleBuilder.java:335) (via modules: ru.test.Main$Module -> com.google.inject.assistedinject.FactoryModuleBuilder$1)
Es ist mein Problem gelöst. Ich dachte, dass Guice @Assisted für mich, aber in der Tat, es ist ich brauche meine Hilfe :) – Andrew
@Andrew Ich bin froh, dass es Ihr Problem verkauft. Könnte es Ihnen denn etwas ausmachen, diese Antwort als akzeptiert zu betrachten? Es klickt auf das Häkchen neben den Punkten der Antwort. Vielen Dank ;-) –