Skip to content

Commit f590511

Browse files
committed
Introspect pre-registered singletons in preDetermineBeanTypes as well
Closes gh-33668
1 parent c4b6a02 commit f590511

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,25 +425,37 @@ public void refreshForAotProcessing(RuntimeHints runtimeHints) {
425425
* @see SmartInstantiationAwareBeanPostProcessor#determineBeanType
426426
*/
427427
private void preDetermineBeanTypes(RuntimeHints runtimeHints) {
428+
List<String> singletons = new ArrayList<>();
429+
List<String> lazyBeans = new ArrayList<>();
430+
431+
// First round: pre-registered singleton instances, if any.
432+
for (String beanName : this.beanFactory.getSingletonNames()) {
433+
Class<?> beanType = this.beanFactory.getType(beanName);
434+
if (beanType != null) {
435+
ClassHintUtils.registerProxyIfNecessary(beanType, runtimeHints);
436+
}
437+
singletons.add(beanName);
438+
}
439+
428440
List<SmartInstantiationAwareBeanPostProcessor> bpps =
429441
PostProcessorRegistrationDelegate.loadBeanPostProcessors(
430442
this.beanFactory, SmartInstantiationAwareBeanPostProcessor.class);
431443

432-
List<String> lazyBeans = new ArrayList<>();
433-
434-
// First round: non-lazy singleton beans in definition order,
444+
// Second round: non-lazy singleton beans in definition order,
435445
// matching preInstantiateSingletons.
436446
for (String beanName : this.beanFactory.getBeanDefinitionNames()) {
437-
BeanDefinition bd = getBeanDefinition(beanName);
438-
if (bd.isSingleton() && !bd.isLazyInit()) {
439-
preDetermineBeanType(beanName, bpps, runtimeHints);
440-
}
441-
else {
442-
lazyBeans.add(beanName);
447+
if (!singletons.contains(beanName)) {
448+
BeanDefinition bd = getBeanDefinition(beanName);
449+
if (bd.isSingleton() && !bd.isLazyInit()) {
450+
preDetermineBeanType(beanName, bpps, runtimeHints);
451+
}
452+
else {
453+
lazyBeans.add(beanName);
454+
}
443455
}
444456
}
445457

446-
// Second round: lazy singleton beans and scoped beans.
458+
// Third round: lazy singleton beans and scoped beans.
447459
for (String beanName : lazyBeans) {
448460
preDetermineBeanType(beanName, bpps, runtimeHints);
449461
}

0 commit comments

Comments
 (0)