我正在使用 PicoContainer,我必须添加一个组件,该组件具有带参数的构造函数。所以我有
public abstract class IA {
@Inject
protected B b;
public void useB(){
b.useSomeMethodOfB();
}
}
public interface IC{}
public class C implements IC{}
public class A extends IA{
private IC mSomeOtherComponent;
public A(IC someOtherComponent){
mSomeOtherComponent = someOtherComponent
}
}
现在要实例化这个组件,我有:
MutablePicoContainer context = new PicoBuilder().withAnnotatedFieldInjection().withCaching().build();
然后
contex.addComponent(A.class, new A(new C()));
但是当我在抽象类中调用 useB() 方法时,它返回 null,它不会注入任何东西。我认为我添加组件的方式不正确。我也试过;
ComponentParameter pr = new ComponentParameter(new C());
context.addComponent(IA.class, A.class, pr);
和
ComponentParameter pr = new ComponentParameter(new C());
context.addComponent(A.class, A.class, pr);
但它说“A对字段B的依赖不满足。
我该如何解决?