Enforce circular reference exception within non-managed thread

Closes gh-34672
This commit is contained in:
Juergen Hoeller
2025-03-28 20:46:09 +01:00
parent 9bf01df230
commit 75e5a75da5
2 changed files with 64 additions and 2 deletions

View File

@@ -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;
}
}