zoukankan      html  css  js  c++  java
  • 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 

     
  • 相关阅读:
    HTTP协议
    python中os模块简介
    什么是HTML
    Django框架
    python递归
    web工程中的各种路径(eclipse开发)
    小白向:web中利用request.getPart()上传文件到服务器
    利用 html+css 画同心圆(concentric circles)——绝对布局与相对布局
    理解z-index和css中的层叠顺序问题(大神技术博的读后感?)
    二、系统初始化
  • 原文地址:https://www.cnblogs.com/javakangkang/p/14039127.html
Copyright © 2011-2022 走看看