我们在服务中使用 guice 进行 DI。显然,对于任何非单例注入,guice 正在调用以下具有同步锁的方法。对非单例对象是否有任何特定要求也应该通过同步锁?
private <T> BindingImpl<T> getJustInTimeBinding(Key<T> key, Errors errors, InjectorImpl.JitLimitation jitType) throws ErrorsException {
boolean jitOverride = isProvider(key) || isTypeLiteral(key);
if (this.options.jitDisabled && jitType == InjectorImpl.JitLimitation.NO_JIT && !jitOverride) {
throw errors.jitDisabled(key).toException();
} else {
synchronized(this.state.lock()) {
for(InjectorImpl injector = this; injector != null; injector = injector.parent) {
BindingImpl<T> binding = (BindingImpl)injector.jitBindings.get(key);
if (binding != null) {
return binding;
}
}
if (this.options.jitDisabled && jitType != InjectorImpl.JitLimitation.NEW_OR_EXISTING_JIT && !jitOverride) {
throw errors.jitDisabled(key).toException();
} else {
return this.createJustInTimeBindingRecursive(key, errors);
}
}
}
}
Guice 版本:4.0