Enforce circular reference exception within non-managed thread
Closes gh-34672
This commit is contained in:
@@ -110,6 +110,9 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
|
||||
/** Names of beans that are currently in lenient creation. */
|
||||
private final Set<String> singletonsInLenientCreation = new HashSet<>();
|
||||
|
||||
/** Map from bean name to actual creation thread for leniently created beans. */
|
||||
private final Map<String, Thread> lenientCreationThreads = new ConcurrentHashMap<>();
|
||||
|
||||
/** Flag that indicates whether we're currently within destroySingletons. */
|
||||
private volatile boolean singletonsCurrentlyInDestruction = false;
|
||||
|
||||
@@ -307,6 +310,9 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
|
||||
if (!this.singletonsInLenientCreation.contains(beanName)) {
|
||||
break;
|
||||
}
|
||||
if (this.lenientCreationThreads.get(beanName) == Thread.currentThread()) {
|
||||
throw ex;
|
||||
}
|
||||
try {
|
||||
this.lenientCreationFinished.await();
|
||||
}
|
||||
@@ -344,7 +350,18 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
|
||||
// Leniently created singleton object could have appeared in the meantime.
|
||||
singletonObject = this.singletonObjects.get(beanName);
|
||||
if (singletonObject == null) {
|
||||
singletonObject = singletonFactory.getObject();
|
||||
if (locked) {
|
||||
singletonObject = singletonFactory.getObject();
|
||||
}
|
||||
else {
|
||||
this.lenientCreationThreads.put(beanName, Thread.currentThread());
|
||||
try {
|
||||
singletonObject = singletonFactory.getObject();
|
||||
}
|
||||
finally {
|
||||
this.lenientCreationThreads.remove(beanName);
|
||||
}
|
||||
}
|
||||
newSingleton = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user