Skip to content

Commit 74dc61b

Browse files
committed
Fall back to local synchronization in getObjectFromFactoryBean
Closes gh-35545
1 parent 717358b commit 74dc61b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/FactoryBeanRegistrySupport.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,17 @@ protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanNam
130130
try {
131131
Object object = this.factoryBeanObjectCache.get(beanName);
132132
if (object == null) {
133-
object = doGetObjectFromFactoryBean(factory, beanName);
133+
if (locked) {
134+
// The common case: within general singleton lock.
135+
object = doGetObjectFromFactoryBean(factory, beanName);
136+
}
137+
else {
138+
// Fall back to local synchronization on the given FactoryBean instance,
139+
// as a defensive measure for non-thread-safe FactoryBean implementations.
140+
synchronized (factory) {
141+
object = doGetObjectFromFactoryBean(factory, beanName);
142+
}
143+
}
134144
// Only post-process and store if not put there already during getObject() call above
135145
// (for example, because of circular reference processing triggered by custom getBean calls)
136146
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);

0 commit comments

Comments
 (0)