spring源码3--AnnotationConfigApplicationContext.register(xxx.class)

spring源码3--AnnotationConfigApplicationContext.register(xxx.class)_第1张图片
debug时发现多了一个我们自己注册的类!!!点进去看看
spring源码3--AnnotationConfigApplicationContext.register(xxx.class)_第2张图片
spring源码3--AnnotationConfigApplicationContext.register(xxx.class)_第3张图片

 this.reader.register(componentClasses);

再点进去:
spring源码3--AnnotationConfigApplicationContext.register(xxx.class)_第4张图片

 this.doRegisterBean(beanClass, (String)null, (Class[])null, (Supplier)null, (BeanDefinitionCustomizer[])null);

spring源码3--AnnotationConfigApplicationContext.register(xxx.class)_第5张图片

 private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name, @Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier, @Nullable BeanDefinitionCustomizer[] customizers) {
     
        AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
        if (!this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
     
            abd.setInstanceSupplier(supplier);
            ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
            abd.setScope(scopeMetadata.getScopeName());
            String beanName = name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry);
            AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
            int var10;
            int var11;
            if (qualifiers != null) {
     
                Class[] var9 = qualifiers;
                var10 = qualifiers.length;

                for(var11 = 0; var11 < var10; ++var11) {
     
                    Class<? extends Annotation> qualifier = var9[var11];
                    if (Primary.class == qualifier) {
     
                        abd.setPrimary(true);
                    } else if (Lazy.class == qualifier) {
     
                        abd.setLazyInit(true);
                    } else {
     
                        abd.addQualifier(new AutowireCandidateQualifier(qualifier));
                    }
                }
            }

            if (customizers != null) {
     
                BeanDefinitionCustomizer[] var13 = customizers;
                var10 = customizers.length;

                for(var11 = 0; var11 < var10; ++var11) {
     
                    BeanDefinitionCustomizer customizer = var13[var11];
                    customizer.customize(abd);
                }
            }

            BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
            definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
            BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
        }
    }

doRegisterBean():就是这个方法做的事情:
在这里插入图片描述
看到最后的这一句:

BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);

点进去:
spring源码3--AnnotationConfigApplicationContext.register(xxx.class)_第6张图片
又发现这个方法啦!!!!!:

registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

破案!!!!!!
总结:自己注册的类在 context.register(Appconfig.class);方法是加入工厂,并且可以添加多个!!
在这里插入图片描述
续:spring源码4–AnnotationConfigApplicationContext.refresh();

你可能感兴趣的:(笔记,spring)