【发布时间】:2014-11-28 10:00:44
【问题描述】:
我正在使用 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 的依赖不满足。
我该如何解决?
【问题讨论】:
-
contex.addComponent(B.class, new A(new C()));? (听说过PicoContainer,但还没用过。) -
我是新手,正如我所说的,我认为这不是正确的做法。
标签: java ioc-container picocontainer