查找并执行带 PriorityOrdered 的 BeanDefinitionRegistryPostProcessor

从注册的 bean 中查找实现 PriorityOrdered 的 BeanDefinitionRegistryPostProcessor,实例化并执行他们

  1. // 临时记录当前的处理器
  2. List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
  3. // 1.从缓存中拿实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor
  4. String[] postProcessorNames =
  5. beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
  6. for (String ppName : postProcessorNames) {
  7. //2.判断下类型是否正确,然后实例化
  8. if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
  9. currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
  10. processedBeans.add(ppName);
  11. }
  12. }
  13. // 3.处理器排个序
  14. sortPostProcessors(currentRegistryProcessors, beanFactory);
  15. // 4.记录下处理器
  16. registryProcessors.addAll(currentRegistryProcessors);
  17. // 5.执行处理器
  18. invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
  19. // 清除临时记录的数据
  20. currentRegistryProcessors.clear();

片段:getBeanNamesForType 获取 Bean

根据类型获取 Bean

  1. @Override
  2. public String[] getBeanNamesForType(@Nullable Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
  3. // isConfigurationFrozen:表示已经加载的bean定义是否允许修改
  4. // 而且这里的type应该始终都不会为null
  5. // 启动阶段isConfigurationFrozen应该是默认值false
  6. if (!isConfigurationFrozen() || type == null || !allowEagerInit) {
  7. // ResolvableType 参考附录:ResolvableType 泛型操作类
  8. return doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, allowEagerInit);
  9. }
  10. Map<Class<?>, String[]> cache =
  11. (includeNonSingletons ? this.allBeanNamesByType : this.singletonBeanNamesByType);
  12. String[] resolvedBeanNames = cache.get(type);
  13. if (resolvedBeanNames != null) {
  14. return resolvedBeanNames;
  15. }
  16. resolvedBeanNames = doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, true);
  17. if (ClassUtils.isCacheSafe(type, getBeanClassLoader())) {
  18. cache.put(type, resolvedBeanNames);
  19. }
  20. return resolvedBeanNames;
  21. }

执行根据类型查找 bean 定义

  1. private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
  2. List<String> result = new ArrayList<>();
  3. // Check all bean definitions.
  4. for (String beanName : this.beanDefinitionNames) {
  5. // Only consider bean as eligible if the bean name is not defined as alias for some other bean.
  6. if (!isAlias(beanName)) {
  7. try {
  8. RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
  9. // Only check bean definition if it is complete.
  10. if (!mbd.isAbstract() && (allowEagerInit ||
  11. (mbd.hasBeanClass() || !mbd.isLazyInit() || isAllowEagerClassLoading()) &&
  12. !requiresEagerInitForType(mbd.getFactoryBeanName()))) {
  13. boolean isFactoryBean = isFactoryBean(beanName, mbd);
  14. BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
  15. boolean matchFound = false;
  16. boolean allowFactoryBeanInit = (allowEagerInit || containsSingleton(beanName));
  17. boolean isNonLazyDecorated = (dbd != null && !mbd.isLazyInit());
  18. if (!isFactoryBean) {
  19. if (includeNonSingletons || isSingleton(beanName, mbd, dbd)) {
  20. matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
  21. }
  22. }
  23. else {
  24. if (includeNonSingletons || isNonLazyDecorated ||
  25. (allowFactoryBeanInit && isSingleton(beanName, mbd, dbd))) {
  26. matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
  27. }
  28. if (!matchFound) {
  29. // In case of FactoryBean, try to match FactoryBean instance itself next.
  30. beanName = FACTORY_BEAN_PREFIX + beanName;
  31. matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
  32. }
  33. }
  34. if (matchFound) {
  35. result.add(beanName);
  36. }
  37. }
  38. }
  39. catch (CannotLoadBeanClassException | BeanDefinitionStoreException ex) {
  40. if (allowEagerInit) {
  41. throw ex;
  42. }
  43. // Probably a placeholder: let's ignore it for type matching purposes.
  44. LogMessage message = (ex instanceof CannotLoadBeanClassException ?
  45. LogMessage.format("Ignoring bean class loading failure for bean '%s'", beanName) :
  46. LogMessage.format("Ignoring unresolvable metadata in bean definition '%s'", beanName));
  47. logger.trace(message, ex);
  48. // Register exception, in case the bean was accidentally unresolvable.
  49. onSuppressedException(ex);
  50. }
  51. catch (NoSuchBeanDefinitionException ex) {
  52. // Bean definition got removed while we were iterating -> ignore.
  53. }
  54. }
  55. }
  56. // Check manually registered singletons too.
  57. for (String beanName : this.manualSingletonNames) {
  58. try {
  59. // In case of FactoryBean, match object created by FactoryBean.
  60. if (isFactoryBean(beanName)) {
  61. if ((includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type)) {
  62. result.add(beanName);
  63. // Match found for this bean: do not match FactoryBean itself anymore.
  64. continue;
  65. }
  66. // In case of FactoryBean, try to match FactoryBean itself next.
  67. beanName = FACTORY_BEAN_PREFIX + beanName;
  68. }
  69. // Match raw bean instance (might be raw FactoryBean).
  70. if (isTypeMatch(beanName, type)) {
  71. result.add(beanName);
  72. }
  73. }
  74. catch (NoSuchBeanDefinitionException ex) {
  75. // Shouldn't happen - probably a result of circular reference resolution...
  76. logger.trace(LogMessage.format(
  77. "Failed to check manually registered singleton with name '%s'", beanName), ex);
  78. }
  79. }
  80. return StringUtils.toStringArray(result);
  81. }

片段:isTypeMatch 判断 Bean 类型

  1. for (String ppName : postProcessorNames) {
  2. //2.判断下类型是否正确,然后实例化
  3. if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
  4. currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
  5. processedBeans.add(ppName);
  6. }
  7. }

片段:getBean

实例化 bean。这里参考:bean 实例化阶段 getBean

片段:sortPostProcessors 排序 Bean

bean 排序。

片段:invokeBeanDefinitionRegistryPostProcessors 执行 Bean 方法

执行方法。