Skip to content

Commit ae9abc8

Browse files
committed
Merge branch '6.2.x'
2 parents d28c039 + 467d5f3 commit ae9abc8

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -277,7 +277,25 @@ public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
277277
if (logger.isDebugEnabled()) {
278278
logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
279279
}
280-
beforeSingletonCreation(beanName);
280+
281+
try {
282+
beforeSingletonCreation(beanName);
283+
}
284+
catch (BeanCurrentlyInCreationException ex) {
285+
if (locked) {
286+
throw ex;
287+
}
288+
// Try late locking for waiting on specific bean to be finished.
289+
this.singletonLock.lock();
290+
locked = true;
291+
// Singleton object should have appeared in the meantime.
292+
singletonObject = this.singletonObjects.get(beanName);
293+
if (singletonObject != null) {
294+
return singletonObject;
295+
}
296+
beforeSingletonCreation(beanName);
297+
}
298+
281299
boolean newSingleton = false;
282300
boolean recordSuppressedExceptions = (locked && this.suppressedExceptions == null);
283301
if (recordSuppressedExceptions) {

spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryLockingTests.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@
2323
import org.springframework.beans.testfixture.beans.TestBean;
2424

2525
import static org.assertj.core.api.Assertions.assertThat;
26-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2726

2827
/**
2928
* @author Juergen Hoeller
@@ -56,9 +55,6 @@ public void setBeanFactory(BeanFactory beanFactory) {
5655
@Override
5756
public void afterPropertiesSet() throws Exception {
5857
Thread thread = new Thread(() -> {
59-
// Fail for circular reference from other thread
60-
assertThatExceptionOfType(BeanCurrentlyInCreationException.class).isThrownBy(() ->
61-
beanFactory.getBean(ThreadDuringInitialization.class));
6258
// Leniently create unrelated other bean outside of singleton lock
6359
assertThat(beanFactory.getBean(TestBean.class).getName()).isEqualTo("tb");
6460
// Creation attempt in other thread was successful

0 commit comments

Comments
 (0)