Mirror<?> mi = Mirror.me(Lang.loadClass(ss[0]));
if (hasNullArg) {
Method m = (Method) Lang.first(mi.findMethods(ss[1],args.length));
if (m == null)
throw new IocException("Factory method not found --> ", iobj.getFactory());
dw.setBorning(new MethodCastingBorning<Object>(m));
} else {
Method m = mi.findMethod(ss[1], args);
dw.setBorning(new MethodBorning<Object>(m));
}
} else {
dw.setBorning((Borning<?>) mirror.getBorning(args));
}
// 如果这个对象是容器中的单例,那么就可以生成实例了
// 这一步非常重要,它解除了字段互相引用的问题
Object obj = null;
if (iobj.isSingleton()) {
obj = dw.born(ing);
op.setObj(obj);
}
// 获得每个字段的注入方式
FieldInjector[] fields = new FieldInjector[iobj.getFields().length];
for (int i = 0; i < fields.length; i++) {
IocField ifld = iobj.getFields()[i];
try {
ValueProxy vp = ing.makeValue(ifld.getValue());
fields[i] = FieldInjector.create(mirror, ifld.getName(), vp, ifld.isOptional());
}
catch (Exception e) {
throw Lang.wrapThrow(e, "Fail to eval Injector for field: '%s'", ifld.getName());
}
}
dw.setFields(fields);
// 如果是单例对象,前面已经生成实例了,在这里需要填充一下它的字段
if (null != obj)
dw.fill(ing, obj);
// 对象创建完毕,如果有 create 事件,调用它
dw.onCreate(obj);
}
// 当异常发生,从 context 里移除 ObjectProxy
catch (Throwable e) {
ing.getContext().remove(iobj.getScope(), ing.getObjectName());
throw new IocException(e, "create ioc bean fail name=%s ioc define:\n%s", ing.getObjectName(), iobj);
}
// 返回
return op;
}