整体位置

image.png
调用DefaultListableBeanFactory.registerBeanDefinition
5 6次
6 1次 入口主文件
10 140多次,包括自动配置类,自定义注解那些(最多)
16 1次org.springframework.aop.config.internalAutoProxyCreator
上游1

  1. public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext implements AnnotationConfigRegistry {
  2. public AnnotationConfigServletWebServerApplicationContext() {
  3. this.reader = new AnnotatedBeanDefinitionReader(this);//1
  4. this.scanner = new ClassPathBeanDefinitionScanner(this);
  5. }
  6. @Override
  7. public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)throws BeanDefinitionStoreException {
  8. //org.springframework.context.annotation.internalConfigurationAnnotationProcessor
  9. //org.springframework.context.annotation.internalAutowiredAnnotationProcessor
  10. //org.springframework.context.annotation.internalCommonAnnotationProcessor
  11. //org.springframework.context.event.internalEventListenerProcessor
  12. //org.springframework.context.event.internalEventListenerFactory
  13. //springbootUploadApplication
  14. this.beanFactory.registerBeanDefinition(beanName, beanDefinition);//5
  15. }
  16. }
  17. public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
  18. Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
  19. Assert.notNull(environment, "Environment must not be null");
  20. this.registry = registry;
  21. this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
  22. AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);//2
  23. }
  24. //注册BeanPostProcessor/BeanFactoryPostProcessor/AutowireCandidateResolver
  25. public abstract class AnnotationConfigUtils {
  26. //3
  27. private static BeanDefinitionHolder registerPostProcessor(BeanDefinitionRegistry registry, RootBeanDefinition definition, String beanName) {
  28. definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
  29. //4
  30. registry.registerBeanDefinition(beanName, definition);//这个registry就是AnnotationConfigServletWebServerApplicationContext,回到了上面
  31. return new BeanDefinitionHolder(definition, beanName);
  32. }
  33. }

上游2
起始11#invokeBeanFactoryPostProcessors
AnnotationConfigServletWebServerApplicationContext.invokeBeanFactoryPostProcessors
PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors #invokeBeanDefinitionRegistryPostProcessors
ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry #processConfigBeanDefinitions
分支1 自定义@Controller @Configuration
ConfigurationClassParser#parse #processConfigurationClass #doProcessConfigurationClass
ComponentScanAnnotationParser#parse
ClassPathBeanDefinitionScanner#doScan #registerBeanDefinition
BeanDefinitionReaderUtils#registerBeanDefinition
终于DefaultListableBeanFactory.registerBeanDefinition
分支2 myServlet myFilter myListener servletComponentRegisteringPostProcessor
ConfigurationClassBeanDefinitionReader#loadBeanDefinitions 遍历#loadBeanDefinitionsForConfigurationClass #loadBeanDefinitionsForBeanMethod #this.registry.registerBeanDefinition(beanName, beanDefToRegister);
终于DefaultListableBeanFactory.registerBeanDefinition
分支3
ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsForConfigurationClass #loadBeanDefinitionsFromRegistrars
ServletComponentScanRegistrar#registerBeanDefinitions #registerBeanDefinitions #addPostProcessor
终于DefaultListableBeanFactory.registerBeanDefinition
分支4 org.springframework.boot.autoconfigure.AutoConfigurationPackages
ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsFromRegistrars
org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar#registerBeanDefinitions #registerBeanDefinitions #register
终于DefaultListableBeanFactory.registerBeanDefinition
分支5 org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration

分支6 dispatcherServlet
ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsForBeanMethod
核心代码

  1. 作为AnnotationConfigServletWebServerApplicationContext的一个属性
  2. public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
  3. /** List of bean definition names, in registration order.存储Bean名称 */
  4. private volatile List<String> beanDefinitionNames = new ArrayList<>(256);
  5. /** Map of bean definition objects, keyed by bean name. map存储Bean名称和Bean定义*/
  6. private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
  7. //6
  8. @Override
  9. public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException {
  10. //所有注册Bean都会经过这里!!
  11. //部分逻辑
  12. if (hasBeanCreationStarted()) {
  13. // Cannot modify startup-time collection elements anymore (for stable iteration)
  14. synchronized (this.beanDefinitionMap) {
  15. this.beanDefinitionMap.put(beanName, beanDefinition);
  16. List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
  17. updatedDefinitions.addAll(this.beanDefinitionNames);
  18. updatedDefinitions.add(beanName);
  19. this.beanDefinitionNames = updatedDefinitions;
  20. removeManualSingletonName(beanName);
  21. }
  22. }
  23. else {
  24. // Still in startup registration phase
  25. this.beanDefinitionMap.put(beanName, beanDefinition);
  26. this.beanDefinitionNames.add(beanName);
  27. removeManualSingletonName(beanName);
  28. }
  29. }
  30. }

总结下关系:

  • 所有注册Bean的入口就是DefaultListableBeanFactory#registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
  • 真正的容器在于AnnotationConfigServletWebServerApplicationContext#属性private final DefaultListableBeanFactory beanFactory;

    类图

    image.png
    应用vs容器
    1. 真正类
    2. public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext
    3. implements AnnotationConfigRegistry
    4. 祖父
    5. public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
    6. //存放Bean对象的东西
    7. private final DefaultListableBeanFactory beanFactory;
    8. }
    9. //存放Bean对象的东西
    10. public class DefaultListableBeanFactory{
    11. /** Map from dependency type to corresponding autowired value. */
    12. private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16);
    13. /** Map of bean definition objects, keyed by bean name. */
    14. private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
    15. /** Map from bean name to merged BeanDefinitionHolder. */
    16. private final Map<String, BeanDefinitionHolder> mergedBeanDefinitionHolders = new ConcurrentHashMap<>(256);
    17. /** Map of singleton and non-singleton bean names, keyed by dependency type. */
    18. private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64);
    19. /** Map of singleton-only bean names, keyed by dependency type. */
    20. private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<>(64);
    21. /** List of bean definition names, in registration order. */
    22. private volatile List<String> beanDefinitionNames = new ArrayList<>(256);
    23. /** List of names of manually registered singletons, in registration order. */
    24. private volatile Set<String> manualSingletonNames = new LinkedHashSet<>(16);
    25. /** Cached array of bean definition names in case of frozen configuration. */
    26. @Nullable
    27. private volatile String[] frozenBeanDefinitionNames;
    28. }

桃园三结义

AnnotationConfigServletWebServerApplicationContext

BeanDefinition

BeanPostProcessor