Notes on Spring5 source (1.4.1) getSingleton (beanName) Nathan Example instance from the cache

In doGetBean method where we first try to take examples from the cache:

Object sharedInstance = getSingleton(beanName);

With source code:
class DefaultSingletonBeanRegistry
Here Insert Picture Description
with getSingleton:

@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
	//先从一级缓存拿
	Object singletonObject = this.singletonObjects.get(beanName);
	//如果bean正在创建。堆内存有了,属性还没有DI(依赖注入)
	if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
		synchronized (this.singletonObjects) {
			//从二级缓存中拿
			singletonObject = this.earlySingletonObjects.get(beanName);

			//如果还拿不到,并且允许bean提前引用(解决循环依赖)
			if (singletonObject == null && allowEarlyReference) {
				//从三级缓存中拿到对象工厂
				ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
				if (singletonFactory != null) {
					//从工厂中拿到对象
					singletonObject = singletonFactory.getObject();
					//升级到二级缓存
					this.earlySingletonObjects.put(beanName, singletonObject);
					//删除三级缓存
					this.singletonFactories.remove(beanName);
				}
			}
		}
	}
	return singletonObject;
}

Introduction This same class of several containers:

//一级缓存
/** Cache of singleton objects: bean name to bean instance. */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
//三级缓存
/** Cache of singleton factories: bean name to ObjectFactory. */
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
//二级缓存
/** Cache of early singleton objects: bean name to bean instance. */
private final Map<String, Object> earlySingletonObjects = new HashMap<>(16);
//正在实例化的bean
/** Names of beans that are currently in creation. */
private final Set<String> singletonsCurrentlyInCreation =
		Collections.newSetFromMap(new ConcurrentHashMap<>(16));

As for why should three cache, a very important role is to resolve circular dependencies.
How to solve temporarily not in this section focuses on.

Published 130 original articles · won praise 233 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44367006/article/details/104400511