spring中类型注解下的bean的加载顺序

基于java注解的方式配置的Appconfig中不同注解的bean的加载顺序:
  注册的顺序: @ComponentScan > 实现ImportSelector  > 方法bean  >  @ImportResource("spring.xml") > 实现 ImportBeanDefinitionRegistrar  (相对的顺序,都在同一个配置类上配置)
@Nullable
    protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
            throws IOException {
 
        if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
            // Recursively process any member (nested) classes first
            processMemberClasses(configClass, sourceClass);
        }
 
        // Process any @PropertySource annotations
        // 处理 @PropertySource    properties  key=value
        for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
                sourceClass.getMetadata(), PropertySources.class,
                org.springframework.context.annotation.PropertySource.class)) {
            if (this.environment instanceof ConfigurableEnvironment) {
                processPropertySource(propertySource);
            }
            else {
                logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
                        "]. Reason: Environment must implement ConfigurableEnvironment");
            }
        }
 
        // Process any @ComponentScan annotations
        //  处理@ComponentScan   将@Component修饰的bean注册到容器
        Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
                sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
        if (!componentScans.isEmpty() &&
                !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
            for (AnnotationAttributes componentScan : componentScans) {
                // The config class is annotated with @ComponentScan -> perform the scan immediately
                // @ComponentScan扫描bean,返回@Component修饰的BeanDefinitionHolder 集合,并且
                // 会将bean注册到容器
                Set<BeanDefinitionHolder> scannedBeanDefinitions =
                        this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
                // Check the set of scanned definitions for any further config classes and parse recursively if needed
                for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
                    BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
                    if (bdCand == null) {
                        bdCand = holder.getBeanDefinition();
                    }
                    if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
                        parse(bdCand.getBeanClassName(), holder.getBeanName());
                    }
                }
            }
        }
 
        // Process any @Import annotations
        // 处理@Import   implements ImportSelector  并不会将bean注册到容器
        processImports(configClass, sourceClass, getImports(sourceClass), true);
 
        // Process any @ImportResource annotations
        // 处理@ImportResource   bean不会注册到容器
        AnnotationAttributes importResource =
                AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
        if (importResource != null) {
            String[] resources = importResource.getStringArray("locations");
            Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
            for (String resource : resources) {
                String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
                configClass.addImportedResource(resolvedResource, readerClass);
            }
        }
 
        // Process individual @Bean methods
        // 处理 bean method    bean不会注册到容器
        Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
        for (MethodMetadata methodMetadata : beanMethods) {
            configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
        }
 
        // Process default methods on interfaces
        processInterfaces(configClass, sourceClass);
 
        // Process superclass, if any
        if (sourceClass.getMetadata().hasSuperClass()) {
            String superclass = sourceClass.getMetadata().getSuperClassName();
            if (superclass != null && !superclass.startsWith("java") &&
                    !this.knownSuperclasses.containsKey(superclass)) {
                this.knownSuperclasses.put(superclass, configClass);
                // Superclass found, return its annotation metadata and recurse
                return sourceClass.getSuperClass();
            }
        }
 
        // No superclass -> processing is complete
        return null;
    }

 spring初始化bean的时区有两种,一种是在容器启动时全部实例化,一种是在使用的时候具体实例化(也称延迟加载)。尚没证明延迟加载与xml标定顺序有关

参考https://www.cnblogs.com/wl20200316/p/12505234.html 

 
原文地址:https://www.cnblogs.com/javakangkang/p/14039127.html