Consistent local synchronization in getObjectFromFactoryBean
Backport Bot / build (push) Has been cancelled Details
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Has been cancelled Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:17], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:25], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled Details
Deploy Docs / Dispatch docs deployment (push) Has been cancelled Details
Build and Deploy Snapshot / Verify (push) Has been cancelled Details

Closes gh-35545
This commit is contained in:
Juergen Hoeller 2025-10-03 17:29:14 +02:00
parent 332953c9a4
commit ecd3dd8883
1 changed files with 34 additions and 39 deletions

View File

@ -128,53 +128,48 @@ public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanReg
locked = (lockFlag && this.singletonLock.tryLock()); locked = (lockFlag && this.singletonLock.tryLock());
} }
try { try {
Object object = this.factoryBeanObjectCache.get(beanName); // Defensively synchronize against non-thread-safe FactoryBean.getObject() implementations,
if (object == null) { // potentially to be called from a background thread while the main thread currently calls
if (locked) { // the same getObject() method within the singleton lock.
// The common case: within general singleton lock. synchronized (factory) {
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
object = doGetObjectFromFactoryBean(factory, beanName); object = doGetObjectFromFactoryBean(factory, beanName);
} // Only post-process and store if not put there already during getObject() call above
else { // (for example, because of circular reference processing triggered by custom getBean calls)
// Fall back to local synchronization on the given FactoryBean instance, Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
// as a defensive measure for non-thread-safe FactoryBean implementations. if (alreadyThere != null) {
synchronized (factory) { object = alreadyThere;
object = doGetObjectFromFactoryBean(factory, beanName);
} }
} else {
// Only post-process and store if not put there already during getObject() call above if (shouldPostProcess) {
// (for example, because of circular reference processing triggered by custom getBean calls)
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
if (alreadyThere != null) {
object = alreadyThere;
}
else {
if (shouldPostProcess) {
if (locked) {
if (isSingletonCurrentlyInCreation(beanName)) {
// Temporarily return non-post-processed object, not storing it yet
return object;
}
beforeSingletonCreation(beanName);
}
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName,
"Post-processing of FactoryBean's singleton object failed", ex);
}
finally {
if (locked) { if (locked) {
afterSingletonCreation(beanName); if (isSingletonCurrentlyInCreation(beanName)) {
// Temporarily return non-post-processed object, not storing it yet
return object;
}
beforeSingletonCreation(beanName);
}
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName,
"Post-processing of FactoryBean's singleton object failed", ex);
}
finally {
if (locked) {
afterSingletonCreation(beanName);
}
} }
} }
} if (containsSingleton(beanName)) {
if (containsSingleton(beanName)) { this.factoryBeanObjectCache.put(beanName, object);
this.factoryBeanObjectCache.put(beanName, object); }
} }
} }
return object;
} }
return object;
} }
finally { finally {
if (locked) { if (locked) {