T callInContext( ContextualCallable callable ) {
Object[] reference = localContext.get(); //标识1
if (reference[0] == null) {
reference[0] = new InternalContext(this);
try {
return callable.call((InternalContext) reference[0]);
} finally {
// Only remove the context if this call created it.
reference[0] = null;
// WW-3768: ThreadLocal was not removed
localContext.remove();
}
} else {
// Someone else will clean up this context.
return callable.call((InternalContext) reference[0]);
}
}
其中localContext也是ContainerImpl的一个属性,是ThreadLocal型的。ThreadLocal是做什么用的?保证localContext这一属性在同一线程内的各个编程层次共享。
ThreadLocal
我们看到localContext的初始函数就是new一个Object数组,其第0个位置为null;
public T getInstance( final Class type, final String name ) {
return callInContext(new ContextualCallable() {
public T call( InternalContext context ) {
return getInstance(type, name, context);
}
});
}
OK,callInContext这个模板方法最后调用的是getInstance(type, name, context)。
@SuppressWarnings("unchecked")
T getInstance( Class type, String name, InternalContext context ) {
ExternalContext> previous = context.getExternalContext();
Key key = Key.newInstance(type, name);
context.setExternalContext(ExternalContext.newInstance(null, key, this));
try {
InternalFactory o = getFactory(key);
if (o != null) {//标识2
return getFactory(key).create(context);
} else {
return null;
}
} finally {
context.setExternalContext(previous);
}
}
大家看到这里,获取对象已经结束了,不过对标识2处的
void inject( Object o, InternalContext context ) {
List injectors = this.injectors.get(o.getClass());//标识3
for ( Injector injector : injectors ) { //标识4
injector.inject(context, o);
}
}
关于标识3处的缓存
public FieldInjector( ContainerImpl container, Field field, String name )
throws MissingDependencyException {
this.field = field;
//...
Key> key = Key.newInstance(field.getType(), name);
factory = container.getFactory(key);
//...
this.externalContext = ExternalContext.newInstance(field, key, container);
}
可以看到,在构造函数中,我们就是
根据type和name进行对象构造工厂factor的寻址。
public void inject( InternalContext context, Object o ) {
ExternalContext> previous = context.getExternalContext();
context.setExternalContext(externalContext);
field.set(o, factory.create(context));
//省略trycatch
}
同样的field.set(o, factory.create(context));这里大家会有疑问,没事我们一会调试。
public class ContainerImplTest extends TestCase {
private Container c;
@Override
protected void setUp() throws Exception {
super.setUp();
ContainerBuilder cb = new ContainerBuilder();
cb.constant("methodCheck.name", "sss");
cb.constant("fieldCheck.name", "Lukasz");
c = cb.create(false);
}
public void testGetInstance(){
Object o=c.getInstance(String.class,"methodCheck.name");
System.out.println(o+" ");
}
}
输出结果
private ContainerBuilder constant(final Class type, final String name,
final T value) {
InternalFactory factory = new InternalFactory() {
public T create(InternalContext ignored) {
return value; //这个value就是"sss"
}
};
return factory(Key.newInstance(type, name), factory, Scope.DEFAULT);
}
public void testFieldInjector() throws Exception {
FieldCheck fieldCheck = new FieldCheck();
try {
c.inject(fieldCheck);
} catch (DependencyException expected) {
fail("No exception expected!");
}
System.out.println(fieldCheck.getName());
}
class FieldCheck {
//就是说我需要在容器中注册名字为fieldCheck.name的那个元素
@Inject("fieldCheck.name")
private String name;
public String getName() {
return name;
}
}
那什么时候它不为null呢?
感谢glt