Skip to content

Commit 8870e4d

Browse files
committed
fix #710
1 parent 66f529b commit 8870e4d

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

src/org/nutz/ioc/impl/ObjectMakerImpl.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,10 @@ public ObjectProxy make(IocMaking ing, IocObject iobj) {
114114
IocField ifld = iobj.getFields()[i];
115115
try {
116116
ValueProxy vp = ing.makeValue(ifld.getValue());
117-
fields[i] = FieldInjector.create(mirror, ifld.getName(), vp);
117+
fields[i] = FieldInjector.create(mirror, ifld.getName(), vp, ifld.isOptional());
118118
}
119119
catch (Exception e) {
120-
if (!ifld.isOptional()) {
121-
throw Lang.wrapThrow(e, "Fail to eval Injector for field: '%s'", ifld.getName());
122-
}
123-
log.info("ioc field create fail, and it is optional, ignore error", e);
120+
throw Lang.wrapThrow(e, "Fail to eval Injector for field: '%s'", ifld.getName());
124121
}
125122
}
126123
dw.setFields(fields);
@@ -135,11 +132,8 @@ public ObjectProxy make(IocMaking ing, IocObject iobj) {
135132
}
136133
// 当异常发生,从 context 里移除 ObjectProxy
137134
catch (Throwable e) {
138-
if (log.isWarnEnabled())
139-
log.warn(String.format("IobObj: \n%s", iobj.toString()), e);
140135
ing.getContext().remove(iobj.getScope(), ing.getObjectName());
141-
throw new IocException("create ioc bean fail name="
142-
+ ing.getObjectName(), e);
136+
throw new IocException("create ioc bean fail name=" + ing.getObjectName(), e);
143137
}
144138

145139
// 返回

src/org/nutz/ioc/weaver/FieldInjector.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,40 @@
22

33
import org.nutz.ioc.IocMaking;
44
import org.nutz.ioc.ValueProxy;
5+
import org.nutz.lang.Lang;
56
import org.nutz.lang.Mirror;
67
import org.nutz.lang.inject.Injecting;
8+
import org.nutz.log.Log;
9+
import org.nutz.log.Logs;
710

811
public class FieldInjector {
12+
13+
private static final Log log = Logs.get();
914

10-
public static FieldInjector create(Mirror<?> mirror, String fieldName, ValueProxy vp) {
15+
public static FieldInjector create(Mirror<?> mirror, String fieldName, ValueProxy vp, boolean optional) {
1116
FieldInjector fi = new FieldInjector();
1217
fi.valueProxy = vp;
1318
fi.inj = mirror.getInjecting(fieldName);
19+
fi.optional = optional;
1420
return fi;
1521
}
1622

1723
private ValueProxy valueProxy;
1824
private Injecting inj;
25+
private boolean optional;
1926

2027
private FieldInjector() {}
2128

2229
void inject(IocMaking ing, Object obj) {
23-
Object value = valueProxy.get(ing);
24-
inj.inject(obj, value);
30+
try {
31+
Object value = valueProxy.get(ing);
32+
inj.inject(obj, value);
33+
} catch (Throwable e) {
34+
if (optional) {
35+
log.info("field inject fail, but this field is optional, ignore error", e);
36+
return;
37+
}
38+
throw Lang.wrapThrow(e);
39+
}
2540
}
2641
}

0 commit comments

Comments
 (0)