首页 > 编程语言 >项目里出现两个配置类继承WebMvcConfigurationSupport时,为什么只有一个会生效(源码分析)

项目里出现两个配置类继承WebMvcConfigurationSupport时,为什么只有一个会生效(源码分析)

时间:2023-12-22 14:32:23浏览次数:28  
标签:configClass 配置 getMetadata WebMvcConfigurationSupport bean 源码 生效 sourceClass

为什么我们的项目里出现两个配置类继承WebMvcConfigurationSupport时,只有一个会生效。我在网上找了半天都是说结果的,没有人分析源码到底是为啥,博主准备讲解一下,希望可以帮到大家!

  大家基本遇到过一种情况,就是我配置类中已经配置了,为什么就是没有生效呢?其中一种原因就是,自己写的配置类也继承了WebMvcConfigurationSupport,当项目出现两个配置类都继承该类时,只会讲第一个配置类生效,至于为什么,就是今天博主需要讲解的,我们必须了解一些springboot的bean的创建过程也就是其生命周期: https://www.processon.com/view/link/5f704050f346fb166d0f3e3c 虽然画的比较简单,有许多细节都没有解析,但是对于当前我们的话题来讲已经基本可以了;

  第一步:我们的配置类是从哪里开始创建解析的:大家可以看到图示bean的流程中doProcessConfigurationClass(configClass, sourceClass, filter);方法,我们看一下是如何调用它 的:

protected void processConfigurationClass(ConfigurationClass configClass, Predicate<String> filter) throws IOException {
        if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {
            return;
        }

        ConfigurationClass existingClass = this.configurationClasses.get(configClass);
        if (existingClass != null) {
            if (configClass.isImported()) {
                if (existingClass.isImported()) {
                    existingClass.mergeImportedBy(configClass);
                }
                // Otherwise ignore new imported config class; existing non-imported class overrides it.
                return;
            }
            else {
                // Explicit bean definition found, probably replacing an import.
                // Let's remove the old one and go with the new one.
                this.configurationClasses.remove(configClass);
                this.knownSuperclasses.values().removeIf(configClass::equals);
            }
        }

        // Recursively process the configuration class and its superclass hierarchy.
        SourceClass sourceClass = asSourceClass(configClass, filter);
        do {
            //从这里开始解析我们的当前配置类
            sourceClass = doProcessConfigurationClass(configClass, sourceClass, filter);
        }
        while (sourceClass != null);

        this.configurationClasses.put(configClass, configClass);
    }

这里可以看到一个while循环,为什么要这么设计呢?我们再看看doProcessConfigurationClass(configClass, sourceClass, filter);方法的源码

protected final SourceClass doProcessConfigurationClass(
            ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter)
            throws IOException {

        if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
            // Recursively process any member (nested) classes first
            processMemberClasses(configClass, sourceClass, filter);
        }

        // Process any @PropertySource annotations
        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
        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
                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
        processImports(configClass, sourceClass, getImports(sourceClass), filter, true);

        // Process any @ImportResource annotations
        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);
            }
        }
        //这里也很重要,这里开始会解析当前配置类里的bean,然后解析父类里面的bean,就是这里才会把WebMvcConfigurationSupport的所有bean
        //都解析出来并添加到configClass里面,不管解析当前类还是父类,configClass都是自己当前的配置类,所以WebMvcConfigurationSupport
        // Process individual @Bean methods
        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)) {
                //如果我们第一个继承了WebMvcConfigurationSupport的配置类,已经被扫描到,就会添加一个map缓存,
                //下一个也继承了WebMvcConfigurationSupport的配置类,将不在解析,直接返回null。结束循环,这也是外面一层为什么要添加while循环
                this.knownSuperclasses.put(superclass, configClass);
                // Superclass found, return its annotation metadata and recurse
                return sourceClass.getSuperClass();
            }
        }

        // No superclass -> processing is complete
        return null;

所以就现在来讲,基本已经决定了,解析第一个配置类的时候,第二个配置类重写的任何方法基本没什么用了,因为父类所有的bean已经在第一个配置类中解析扫描到了,就剩下如何去创建bean了。我们再继续往下看会更明白;

  第二步:现在当所有bean已经扫描到,并且bean定义已经完成,该开始实例化了,看一下createBeanInstance的创建过程,最后生成的时候会找到 factoryBean也就是我们自己的配置类

private Object instantiate(String beanName, RootBeanDefinition mbd,
            @Nullable Object factoryBean, Method factoryMethod, Object[] args) {

        try {
            if (System.getSecurityManager() != null) {
                return AccessController.doPrivileged((PrivilegedAction<Object>) () ->
                        this.beanFactory.getInstantiationStrategy().instantiate(
                                mbd, beanName, this.beanFactory, factoryBean, factoryMethod, args),
                        this.beanFactory.getAccessControlContext());
            }
            else {
                return this.beanFactory.getInstantiationStrategy().instantiate(
                        mbd, beanName, this.beanFactory, factoryBean, factoryMethod, args);
            }
        }
        catch (Throwable ex) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                    "Bean instantiation via factory method failed", ex);
        }
    }

其中factoryBean就是我们的当前第一个被解析到的配置类bean,截图为证,我自己写了两个配置类,第一个被加载的是MyASD,瞎写的名,好区分,第二个配置类是WebConfiguration,我们只看WebMvcConfigurationSupport里面的其中一个bean的创建过程,就是requestMappingHandlerAdapter,为啥要看这个,正好跟上节json自定义衔接。

https://www.cnblogs.com/guoxiaoyu/p/13667961.html image.png 到这里,我们可以看到在生成requestMappingHandlerAdapter时,调用extendMessageConverters方法时,一定会调用第一个配置类中的重写方法,因为所有的WebMvcConfigurationSupport里面 bean都被第一个配置类解析完了,所有的factoryBean都是当前第一个配置类,就算第二个配置完没有报错,也不会生效了。

我直接把这个问题用源码的方式讲解清楚,方便大家明白为什么配置两个WebMvcConfigurationSupport类,只有一个生效。

标签:configClass,配置,getMetadata,WebMvcConfigurationSupport,bean,源码,生效,sourceClass
From: https://blog.51cto.com/StudiousXiaoYu/8935387

相关文章

  • Spring MVC 源码分析 - HandlerMapping 组件(二)之 HandlerInterceptor 拦截器
    HandlerMapping组件HandlerMapping组件,请求的处理器匹配器,负责为请求找到合适的 HandlerExecutionChain 处理器执行链,包含处理器(handler)和拦截器们(interceptors)handler 处理器是Object类型,可以将其理解成HandlerMethod对象(例如我们使用最多的 @RequestMapping 注解所标......
  • llama的hf源码结构
    上一个博客我们看了rope.那么llama的hidden_states就没啥难点了.再整体把握一下hf里面llama的代码结构.文件是:D:\Users\admin\miniconda3\Lib\site-packages\transformers\models\llama\modeling_llama.py基座:classLlamaModel(LlamaPreTrainedModel):这个模型输入bs,se......
  • 数字化医学影像系统源码,采用C语言开发,支持MPR、CPR、MIP、SSD、VR、VE三维图像处理
    PACS系统是医院影像科室中应用的一种系统,主要用于获取、传输、存档和处理医学影像。它通过各种接口,如模拟、DICOM和网络,以数字化的方式将各种医学影像,如核磁共振、CT扫描、超声波等保存起来,并在需要时能够快速调取和使用。PACS系统还提供了辅助诊断和管理功能,可以在不同的影像设备......
  • llama的rope源码阅读
    关键代码的理解:classLlamaRMSNorm(nn.Module):def__init__(self,hidden_size,eps=1e-6):"""LlamaRMSNormisequivalenttoT5LayerNorm"""super().__init__()self.weight=nn.Parameter(to......
  • 开发医疗陪诊系统源码:搭建安全高效的医患互动平台
    本文将深入探讨开发医疗陪诊系统的源码,以及如何搭建一个安全高效的医患互动平台。一、引言医疗陪诊系统旨在通过技术手段,缩短患者与医生之间的距离,提供更快速、便捷的医疗服务。二、技术选型2.1前端技术在搭建医疗陪诊系统的前端时,我们可以选择使用现代化的前端框架,如Vue.js或React......
  • HydroOJ 从入门到入土(9)源码简易修改记录——卍解!
    随着OJ的使用越来越深入,本强迫症总会觉得一些细节有时候不那么符合自己的习惯,但是想改又无处下手,最终还是走上了修改源码的邪路.目录0.重要1.超级管理员查看自测代码2.超级管理员隐身查看比赛/作业题目3.超级管理员隐身查看比赛题目列表4.关掉客观题的多选题部......
  • centos7上源码安装postgresql 13.6
    1环境描述操作系统:Centos7.6postgresql:13.6安装方式:源码安装2创建用户#groupadd-g2000pgsql#useradd-u2000-gpgsqlpgsql3目录规划#mkdir-p/postgresql/{pgdata,archive,scripts,backup,pg13,soft,pg_log}#chown-Rpgsql:pgsql/postgresql#......
  • 视频监控系统LiteCVR平台配置播放限制时长后并未生效的原因排查
    随着科技的不断发展,视频监控技术已经成为了现代社会中不可或缺的一部分。它的应用范围广泛,涵盖了公共安全、工厂管理、家庭安全等多个领域。有用户在使用中反馈,LiteCVR平台的http-flv、hls、webrtc协议,播放限制时长不起作用,如下图:项目-mark5348安防视频监控LiteCVR平台可拓展......
  • 智慧医疗APP开发指南:医疗陪诊系统源码实战
    本文将深入探讨智慧医疗APP开发中的医疗陪诊系统,重点介绍实际应用中的源码实战经验。 一、技术选型在开发医疗陪诊系统时,合理的技术选型是确保系统高效运行的关键。可以选择采用前后端分离的架构,使用Vue.js作为前端框架,SpringBoot作为后端框架,通过RESTfulAPI进行通信。二、实战经......
  • 记录 | ubuntu源码编译安装/更新boost版本
    一、卸载当前的版本1、查看当前安装的boost版本dpkg-S/usr/include/boost/version.hpp通过上面的命令,你就可以发现boost的版本了,查看结果可能如下:libboost1.54-dev:/usr/include/boost/version.hpp2、删除当前安装的boostsudoapt-getautoremovelibboost1.54-dev这样就可以删......