在java后端服务之间调用时,很容易产生循环依赖,对于spring内部是如何解决循环依赖的,简言之,spring采用缓存机制来解决(三级缓存)。
名称 | 描述 | 级别 |
---|---|---|
singletonObjects | 初始化完成的单例对象缓存 | 一级缓存 |
earlySingletonObjects | 提前曝光的单例对象缓存 | 二级缓存 |
singletonFactories | 单例对象工厂缓存 | 三级缓存 |
singletonsCurrentlyInCreation | 当前正在创建中的单例集合 | 判断继续查找二级缓存的条件 |
主要代码逻辑参考类:
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
核心代码如下:
/**
* Return the (raw) singleton object registered under the given name.
* <p>Checks already instantiated singletons and also allows for an early
* reference to a currently created singleton (resolving a circular reference).
* @param beanName the name of the bean to look for
* @param allowEarlyReference whether early references should be created or not
* @return the registered singleton object, or {@code null} if none found
*/
@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
// Quick check for existing instance without full singleton lock
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
synchronized (this.singletonObjects) {
// Consistent creation of early reference within full singleton lock
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
singletonObject =
this.earlySingletonObjects.get(beanName);
if (singletonObject == null) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject =
singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
}
}
return singletonObject;
}
在调用 getSingleton 方法之前,先调用 beforeSingletonCreation 方法
/**
* Callback before singleton creation.
* <p>The default implementation register the singleton as currently in creation.
* @param beanName the name of the singleton about to be created
* @see #isSingletonCurrentlyInCreation
*/
protected void beforeSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
}
本地常量缓存,用来记录当前bean是否正在创建中
/** Names of beans that are currently in creation. */
private final Set<String> singletonsCurrentlyInCreation = Collections.newSetFromMap(new ConcurrentHashMap<>(16));
而在同名方法 getSingleton(String beanName, ObjectFactory<?> singletonFactory)
中,finally块调用了 afterSingletonCreation(beanName);
/**
* Callback after singleton creation.
* <p>The default implementation marks the singleton as not in creation anymore.
* @param beanName the name of the singleton that has been created
* @see #isSingletonCurrentlyInCreation
*/
protected void afterSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
throw new IllegalStateException("Singleton ‘" + beanName + "‘ isn‘t currently in creation");
}
}
在创建结束后,进行删除。
在bean创建的不同时期,会从缓存中读取bean,在创建bean时,尝试先从一级缓存(singletonObjects)中获取bean,获取为null且判断是正在创建的bean,则继续从二级缓存(earlySingletonObjects)中获取bean,当获取的bean继续为空时,并且允许从singletonFactories通过getObject获取(allowEarlyReference 参数为true时),则通过三级缓存(singletonFactory)继续获取,如果获取到bean,先删除三级缓存中bean,再复制到二级缓存(earlySingletonObjects)中。
接下来的二级缓存到一级缓存的转换则通过 addSingleton 实现,在创建bean时,若一级缓存不存在bean,则直接保存bean到三级缓存。
主要代码如下:
/**
* Add the given singleton object to the singleton cache of this factory.
* <p>To be called for eager registration of singletons.
* @param beanName the name of the bean
* @param singletonObject the singleton object
*/
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
this.singletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
/**
* Add the given singleton factory for building the specified singleton
* if necessary.
* <p>To be called for eager registration of singletons, e.g. to be able to
* resolve circular references.
* @param beanName the name of the bean
* @param singletonFactory the factory for the singleton object
*/
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(singletonFactory, "Singleton factory must not be null");
synchronized (this.singletonObjects) {
if (!this.singletonObjects.containsKey(beanName)) {
this.singletonFactories.put(beanName, singletonFactory);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
}
synchronized (this.singletonObjects) {
if (!this.singletonObjects.containsKey(beanName)) {
this.singletonFactories.put(beanName, singletonFactory);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
原文:https://www.cnblogs.com/xysn/p/14778278.html