|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.lang.reflect.Constructor;
|
| 21 | +import java.util.ArrayList; |
21 | 22 | import java.util.List;
|
22 | 23 | import java.util.concurrent.atomic.AtomicBoolean;
|
23 | 24 | import java.util.function.Supplier;
|
@@ -423,16 +424,37 @@ private void preDetermineBeanTypes(RuntimeHints runtimeHints) {
|
423 | 424 | PostProcessorRegistrationDelegate.loadBeanPostProcessors(
|
424 | 425 | this.beanFactory, SmartInstantiationAwareBeanPostProcessor.class);
|
425 | 426 |
|
| 427 | + List<String> lazyBeans = new ArrayList<>(); |
| 428 | + |
| 429 | + // First round: non-lazy singleton beans in definition order, |
| 430 | + // matching preInstantiateSingletons. |
426 | 431 | for (String beanName : this.beanFactory.getBeanDefinitionNames()) {
|
427 |
| - Class<?> beanType = this.beanFactory.getType(beanName); |
428 |
| - if (beanType != null) { |
429 |
| - ClassHintUtils.registerProxyIfNecessary(beanType, runtimeHints); |
430 |
| - for (SmartInstantiationAwareBeanPostProcessor bpp : bpps) { |
431 |
| - Class<?> newBeanType = bpp.determineBeanType(beanType, beanName); |
432 |
| - if (newBeanType != beanType) { |
433 |
| - ClassHintUtils.registerProxyIfNecessary(newBeanType, runtimeHints); |
434 |
| - beanType = newBeanType; |
435 |
| - } |
| 432 | + BeanDefinition bd = getBeanDefinition(beanName); |
| 433 | + if (bd.isSingleton() && !bd.isLazyInit()) { |
| 434 | + preDetermineBeanType(beanName, bpps, runtimeHints); |
| 435 | + } |
| 436 | + else { |
| 437 | + lazyBeans.add(beanName); |
| 438 | + } |
| 439 | + } |
| 440 | + |
| 441 | + // Second round: lazy singleton beans and scoped beans. |
| 442 | + for (String beanName : lazyBeans) { |
| 443 | + preDetermineBeanType(beanName, bpps, runtimeHints); |
| 444 | + } |
| 445 | + } |
| 446 | + |
| 447 | + private void preDetermineBeanType(String beanName, List<SmartInstantiationAwareBeanPostProcessor> bpps, |
| 448 | + RuntimeHints runtimeHints) { |
| 449 | + |
| 450 | + Class<?> beanType = this.beanFactory.getType(beanName); |
| 451 | + if (beanType != null) { |
| 452 | + ClassHintUtils.registerProxyIfNecessary(beanType, runtimeHints); |
| 453 | + for (SmartInstantiationAwareBeanPostProcessor bpp : bpps) { |
| 454 | + Class<?> newBeanType = bpp.determineBeanType(beanType, beanName); |
| 455 | + if (newBeanType != beanType) { |
| 456 | + ClassHintUtils.registerProxyIfNecessary(newBeanType, runtimeHints); |
| 457 | + beanType = newBeanType; |
436 | 458 | }
|
437 | 459 | }
|
438 | 460 | }
|
|
0 commit comments