Skip to content

Commit 6ade8c5

Browse files
committed
Merge branch '6.2.x'
# Conflicts: # spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
2 parents 6ab4cf4 + c4f66b7 commit 6ade8c5

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
211211
/** Whether bean definition metadata may be cached for all beans. */
212212
private volatile boolean configurationFrozen;
213213

214-
private volatile boolean preInstantiationPhase;
215-
216-
private @Nullable volatile String mainThreadPrefix;
214+
/** Name prefix of main thread: only set during pre-instantiation phase. */
215+
private volatile @Nullable String mainThreadPrefix;
217216

218217
private final NamedThreadLocal<PreInstantiation> preInstantiationThread =
219218
new NamedThreadLocal<>("Pre-instantiation thread marker");
@@ -1048,26 +1047,37 @@ protected void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName
10481047

10491048
@Override
10501049
protected @Nullable Boolean isCurrentThreadAllowedToHoldSingletonLock() {
1051-
if (this.preInstantiationPhase) {
1050+
String mainThreadPrefix = this.mainThreadPrefix;
1051+
if (this.mainThreadPrefix != null) {
10521052
// We only differentiate in the preInstantiateSingletons phase.
1053+
10531054
PreInstantiation preInstantiation = this.preInstantiationThread.get();
10541055
if (preInstantiation != null) {
1055-
// A Spring-managed thread:
1056+
// A Spring-managed bootstrap thread:
10561057
// MAIN is allowed to lock (true) or even forced to lock (null),
10571058
// BACKGROUND is never allowed to lock (false).
10581059
return switch (preInstantiation) {
10591060
case MAIN -> (Boolean.TRUE.equals(this.strictLocking) ? null : true);
10601061
case BACKGROUND -> false;
10611062
};
10621063
}
1063-
if (Boolean.FALSE.equals(this.strictLocking) ||
1064-
(this.strictLocking == null && !getThreadNamePrefix().equals(this.mainThreadPrefix))) {
1065-
// An unmanaged thread (assumed to be application-internal) with lenient locking,
1066-
// and not part of the same thread pool that provided the main bootstrap thread
1067-
// (excluding scenarios where we are hit by multiple external bootstrap threads).
1064+
1065+
// Not a Spring-managed bootstrap thread...
1066+
if (Boolean.FALSE.equals(this.strictLocking)) {
1067+
// Explicitly configured to use lenient locking wherever possible.
10681068
return true;
10691069
}
1070+
else if (this.strictLocking == null) {
1071+
// No explicit locking configuration -> infer appropriate locking.
1072+
if (mainThreadPrefix != null && !getThreadNamePrefix().equals(mainThreadPrefix)) {
1073+
// An unmanaged thread (assumed to be application-internal) with lenient locking,
1074+
// and not part of the same thread pool that provided the main bootstrap thread
1075+
// (excluding scenarios where we are hit by multiple external bootstrap threads).
1076+
return true;
1077+
}
1078+
}
10701079
}
1080+
10711081
// Traditional behavior: forced to always hold a full lock.
10721082
return null;
10731083
}
@@ -1085,7 +1095,6 @@ public void preInstantiateSingletons() throws BeansException {
10851095
// Trigger initialization of all non-lazy singleton beans...
10861096
List<CompletableFuture<?>> futures = new ArrayList<>();
10871097

1088-
this.preInstantiationPhase = true;
10891098
this.preInstantiationThread.set(PreInstantiation.MAIN);
10901099
this.mainThreadPrefix = getThreadNamePrefix();
10911100
try {
@@ -1102,7 +1111,6 @@ public void preInstantiateSingletons() throws BeansException {
11021111
finally {
11031112
this.mainThreadPrefix = null;
11041113
this.preInstantiationThread.remove();
1105-
this.preInstantiationPhase = false;
11061114
}
11071115

11081116
if (!futures.isEmpty()) {

0 commit comments

Comments
 (0)