|
2 | 2 |
|
3 | 3 | import org.nutz.ioc.IocMaking; |
4 | 4 | import org.nutz.ioc.ValueProxy; |
| 5 | +import org.nutz.lang.Lang; |
5 | 6 | import org.nutz.lang.Mirror; |
6 | 7 | import org.nutz.lang.inject.Injecting; |
| 8 | +import org.nutz.log.Log; |
| 9 | +import org.nutz.log.Logs; |
7 | 10 |
|
8 | 11 | public class FieldInjector { |
| 12 | + |
| 13 | + private static final Log log = Logs.get(); |
9 | 14 |
|
10 | | - public static FieldInjector create(Mirror<?> mirror, String fieldName, ValueProxy vp) { |
| 15 | + public static FieldInjector create(Mirror<?> mirror, String fieldName, ValueProxy vp, boolean optional) { |
11 | 16 | FieldInjector fi = new FieldInjector(); |
12 | 17 | fi.valueProxy = vp; |
13 | 18 | fi.inj = mirror.getInjecting(fieldName); |
| 19 | + fi.optional = optional; |
14 | 20 | return fi; |
15 | 21 | } |
16 | 22 |
|
17 | 23 | private ValueProxy valueProxy; |
18 | 24 | private Injecting inj; |
| 25 | + private boolean optional; |
19 | 26 |
|
20 | 27 | private FieldInjector() {} |
21 | 28 |
|
22 | 29 | 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 | + } |
25 | 40 | } |
26 | 41 | } |
0 commit comments