按下F5快捷键进入finishBeanFactoryInitialization方法里面,如下图所示,可以看到一开始有跟类型转换组件有关的东东,这个嘛玩意我们也勿须深究,故略过。
image.png
继续按下F6快捷键让程序往下运行,这不是来判断beanFactory是否有值解析器的吗?这一块我们也没必要深究,也略过。
image.png
继续按下F6快捷键让程序往下运行,可以看到有跟AspectJ Weaver有关的东东,这些东东我们也没必要深究,也略过。
image.png
当程序运行至finishBeanFactoryInitialization方法的最后一行代码时,可以很清楚地从该行代码上的注释看出,这儿是来初始化所有剩下的单实例bean的。
image.png

preInstantiateSingletons

// Trigger initialization of all non-lazy singleton beans…

// Trigger post-initialization callback for all applicable beans…

  1. @Override
  2. public void preInstantiateSingletons() throws BeansException {
  3. if (logger.isTraceEnabled()) {
  4. logger.trace("Pre-instantiating singletons in " + this);
  5. }
  6. // Iterate over a copy to allow for init methods which in turn register new bean definitions.
  7. // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
  8. List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
  9. // Trigger initialization of all non-lazy singleton beans...
  10. for (String beanName : beanNames) {
  11. RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
  12. if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
  13. if (isFactoryBean(beanName)) {
  14. Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
  15. if (bean instanceof FactoryBean) {
  16. FactoryBean<?> factory = (FactoryBean<?>) bean;
  17. boolean isEagerInit;
  18. if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
  19. isEagerInit = AccessController.doPrivileged(
  20. (PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
  21. getAccessControlContext());
  22. }
  23. else {
  24. isEagerInit = (factory instanceof SmartFactoryBean &&
  25. ((SmartFactoryBean<?>) factory).isEagerInit());
  26. }
  27. if (isEagerInit) {
  28. getBean(beanName);
  29. }
  30. }
  31. }
  32. else {
  33. getBean(beanName);
  34. }
  35. }
  36. }
  37. // Trigger post-initialization callback for all applicable beans...
  38. for (String beanName : beanNames) {
  39. Object singletonInstance = getSingleton(beanName);
  40. if (singletonInstance instanceof SmartInitializingSingleton) {
  41. SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
  42. if (System.getSecurityManager() != null) {
  43. AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
  44. smartSingleton.afterSingletonsInstantiated();
  45. return null;
  46. }, getAccessControlContext());
  47. }
  48. else {
  49. smartSingleton.afterSingletonsInstantiated();
  50. }
  51. }
  52. }
  53. }