Skip to content

Commit 6682d75

Browse files
committed
Merge branch '6.1.x'
2 parents 75a5409 + 4a10bc3 commit 6682d75

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.lang.reflect.Constructor;
21+
import java.util.ArrayList;
2122
import java.util.List;
2223
import java.util.concurrent.atomic.AtomicBoolean;
2324
import java.util.function.Supplier;
@@ -428,16 +429,37 @@ private void preDetermineBeanTypes(RuntimeHints runtimeHints) {
428429
PostProcessorRegistrationDelegate.loadBeanPostProcessors(
429430
this.beanFactory, SmartInstantiationAwareBeanPostProcessor.class);
430431

432+
List<String> lazyBeans = new ArrayList<>();
433+
434+
// First round: non-lazy singleton beans in definition order,
435+
// matching preInstantiateSingletons.
431436
for (String beanName : this.beanFactory.getBeanDefinitionNames()) {
432-
Class<?> beanType = this.beanFactory.getType(beanName);
433-
if (beanType != null) {
434-
ClassHintUtils.registerProxyIfNecessary(beanType, runtimeHints);
435-
for (SmartInstantiationAwareBeanPostProcessor bpp : bpps) {
436-
Class<?> newBeanType = bpp.determineBeanType(beanType, beanName);
437-
if (newBeanType != beanType) {
438-
ClassHintUtils.registerProxyIfNecessary(newBeanType, runtimeHints);
439-
beanType = newBeanType;
440-
}
437+
BeanDefinition bd = getBeanDefinition(beanName);
438+
if (bd.isSingleton() && !bd.isLazyInit()) {
439+
preDetermineBeanType(beanName, bpps, runtimeHints);
440+
}
441+
else {
442+
lazyBeans.add(beanName);
443+
}
444+
}
445+
446+
// Second round: lazy singleton beans and scoped beans.
447+
for (String beanName : lazyBeans) {
448+
preDetermineBeanType(beanName, bpps, runtimeHints);
449+
}
450+
}
451+
452+
private void preDetermineBeanType(String beanName, List<SmartInstantiationAwareBeanPostProcessor> bpps,
453+
RuntimeHints runtimeHints) {
454+
455+
Class<?> beanType = this.beanFactory.getType(beanName);
456+
if (beanType != null) {
457+
ClassHintUtils.registerProxyIfNecessary(beanType, runtimeHints);
458+
for (SmartInstantiationAwareBeanPostProcessor bpp : bpps) {
459+
Class<?> newBeanType = bpp.determineBeanType(beanType, beanName);
460+
if (newBeanType != beanType) {
461+
ClassHintUtils.registerProxyIfNecessary(newBeanType, runtimeHints);
462+
beanType = newBeanType;
441463
}
442464
}
443465
}

0 commit comments

Comments
 (0)