Я могу добиться, используя ниже шаблон. Может быть, есть более простой способ. Рад узнать больше
A. java
public interface A {
String aMethod();
}
AImpl. java
public class AImpl implements A {
private String moduleName;
public AImpl(String moduleName) {
this.moduleName = moduleName;
}
@Override
public String aMethod() {
return moduleName;
}
}
B. java
public interface B {
String bMethod();
}
Bimpl. java
public class BImpl implements B {
private final A a;
BImpl(A a) {
this.a = a;
}
@Override
public String bMethod() {
return a.aMethod();
}
}
AnAnnot. java
@Target(PARAMETER)
@Retention(RUNTIME)
@BindingAnnotation
public @interface AnAnnot {
}
BProvider. java
public class BProvider implements Provider<B> {
private final A a;
@Inject
BProvider(A a) {
this.a = a;
}
@Override
public B get() {
return new BImpl(a);
}
}
BHavingAnnotatedA. java
public class BHavingAnnotatedA implements Provider<B> {
private final A a;
@Inject
BHavingAnnotatedA(@AnAnnot A a) {
this.a = a;
}
@Override
public B get() {
return new BImpl(a);
}
}
ABModule1. java
public class ABModule1 extends AbstractModule {
@Override
protected void configure() {
bind(A.class).to(AImpl.class);
bind(B.class).toProvider(BProvider.class);
}
}
ABModule2. java
public class ABModule2 extends AbstractModule {
@Override
protected void configure() {
Key<A> aKey = Key.get(A.class, AnAnnot.class);
bind(aKey).to(AImpl.class);
Key<B> bKey = Key.get(B.class, AnAnnot.class);
bind(bKey).toProvider(BHavingAnnotatedA.class);
}
}