带着问题看SpringBoot

news2024/12/23 17:45:11

带着问题看SpringBoot

1、Spring容器具体是什么?

跟进run方法,context = this.createApplicationContext(),得出容器是AnnotationConfigServletWebServerApplicationContext类。

SpringApplication.run(ServeroneApplication.class, args);
   public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            listeners.started(context);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }
    protected ConfigurableApplicationContext createApplicationContext() {
        Class<?> contextClass = this.applicationContextClass;
        if (contextClass == null) {
            try {
                switch (this.webApplicationType) {
                    case SERVLET:
                        contextClass = Class.forName("org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext");
                        break;
                    case REACTIVE:
                        contextClass = Class.forName("org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext");
                        break;
                    default:
                        contextClass = Class.forName("org.springframework.context.annotation.AnnotationConfigApplicationContext");
                }
            } catch (ClassNotFoundException var3) {
                throw new IllegalStateException("Unable create a default ApplicationContext, please specify an ApplicationContextClass", var3);
            }
        }

        return (ConfigurableApplicationContext)BeanUtils.instantiateClass(contextClass);
    }
2、SpringBoot中Tomcat的启动流程。

2.1、跟进TomcatWebServer类的initialize() 方法,断点到logger.info("Tomcat initialized with port(s): " + this.getPortsDescription(false));然后反向debug你可以看出Tomcat的初始化过程,是在容器执行run()方法的时候执行refresh()方法的this.onRefresh();的时候初始化的。

   private void initialize() throws WebServerException {
        logger.info("Tomcat initialized with port(s): " + this.getPortsDescription(false));
        synchronized(this.monitor) {
            try {
                this.addInstanceIdToEngineName();
                Context context = this.findContext();
                context.addLifecycleListener((event) -> {
                    if (context.equals(event.getSource()) && "start".equals(event.getType())) {
                        this.removeServiceConnectors();
                    }

                });
                this.tomcat.start();
                this.rethrowDeferredStartupExceptions();

                try {
                    ContextBindings.bindClassLoader(context, context.getNamingToken(), this.getClass().getClassLoader());
                } catch (NamingException var5) {
                }

                this.startDaemonAwaitThread();
            } catch (Exception var6) {
                this.stopSilently();
                this.destroySilently();
                throw new WebServerException("Unable to start embedded Tomcat", var6);
            }

        }
    }
    public void refresh() throws BeansException, IllegalStateException {
        synchronized(this.startupShutdownMonitor) {
            StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
            this.prepareRefresh();
            ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
            this.prepareBeanFactory(beanFactory);

            try {
                this.postProcessBeanFactory(beanFactory);
                StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
                this.invokeBeanFactoryPostProcessors(beanFactory);
                this.registerBeanPostProcessors(beanFactory);
                beanPostProcess.end();
                this.initMessageSource();
                this.initApplicationEventMulticaster();
                //Tomcat初始化
                this.onRefresh();
                this.registerListeners();
                this.finishBeanFactoryInitialization(beanFactory);
                this.finishRefresh();
            } catch (BeansException var10) {
                if (this.logger.isWarnEnabled()) {
                    this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var10);
                }

                this.destroyBeans();
                this.cancelRefresh(var10);
                throw var10;
            } finally {
                this.resetCommonCaches();
                contextRefresh.end();
            }

        }
    }
3、SpringBoot加载配置文件。

3.1、跟进run()到SpringApplication类的run()方法的ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);

SpringApplication.run(TestApplication.class, args);

3.2、跟进prepareEnvironment()—>listeners.environmentPrepared(bootstrapContext, (ConfigurableEnvironment)environment);—>SimpleApplicationEventMulticaster类的doInvokeListener()方法。

    private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
        try {
            listener.onApplicationEvent(event);
        } catch (ClassCastException var6) {
            String msg = var6.getMessage();
            if (msg != null && !this.matchesClassCastMessage(msg, event.getClass())) {
                throw var6;
            }

            Log logger = LogFactory.getLog(this.getClass());
            if (logger.isTraceEnabled()) {
                logger.trace("Non-matching event type for listener: " + listener, var6);
            }
        }

    }

当listener为EnvironmentPostProcessorApplicationListener的时候,进入onApplicationEvent()方法。

 public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            this.onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent)event);
        }

        if (event instanceof ApplicationPreparedEvent) {
            this.onApplicationPreparedEvent((ApplicationPreparedEvent)event);
        }

        if (event instanceof ApplicationFailedEvent) {
            this.onApplicationFailedEvent((ApplicationFailedEvent)event);
        }

    }

跟进onApplicationEnvironmentPreparedEvent()方法。

  private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
        ConfigurableEnvironment environment = event.getEnvironment();
        SpringApplication application = event.getSpringApplication();
        Iterator var4 = this.getEnvironmentPostProcessors(event.getBootstrapContext()).iterator();

        while(var4.hasNext()) {
            EnvironmentPostProcessor postProcessor = (EnvironmentPostProcessor)var4.next();
            postProcessor.postProcessEnvironment(environment, application);
        }

    }

跟进到ConfigDataEnvironmentPostProcessor类的postProcessEnvironment()。

 void postProcessEnvironment(ConfigurableEnvironment environment, ResourceLoader resourceLoader, Collection<String> additionalProfiles) {
        try {
            this.logger.trace("Post-processing environment to add config data");
            ResourceLoader resourceLoader = resourceLoader != null ? resourceLoader : new DefaultResourceLoader();
            this.getConfigDataEnvironment(environment, (ResourceLoader)resourceLoader, additionalProfiles).processAndApply();
        } catch (UseLegacyConfigProcessingException var5) {
            this.logger.debug(LogMessage.format("Switching to legacy config file processing [%s]", var5.getConfigurationProperty()));
            this.postProcessUsingLegacyApplicationListener(environment, resourceLoader);
        }

    }

跟进processAndApply()方法。

  void processAndApply() {
        ConfigDataImporter importer = new ConfigDataImporter(this.logFactory, this.notFoundAction, this.resolvers, this.loaders);
        this.bootstrapContext.register(Binder.class, InstanceSupplier.from(() -> {
            return this.contributors.getBinder((ConfigDataActivationContext)null, new ConfigDataEnvironmentContributors.BinderOption[]{BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE});
        }));
        ConfigDataEnvironmentContributors contributors = this.processInitial(this.contributors, importer);
        Binder initialBinder = contributors.getBinder((ConfigDataActivationContext)null, new ConfigDataEnvironmentContributors.BinderOption[]{BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE});
        this.bootstrapContext.register(Binder.class, InstanceSupplier.of(initialBinder));
        ConfigDataActivationContext activationContext = this.createActivationContext(initialBinder);
        contributors = this.processWithoutProfiles(contributors, importer, activationContext);
        activationContext = this.withProfiles(contributors, activationContext);
        contributors = this.processWithProfiles(contributors, importer, activationContext);
        this.applyToEnvironment(contributors, activationContext);
    }

跟进this.processInitial(this.contributors, importer)方法->withProcessedImports()方法->importer.resolveAndLoad(activationContext, locationResolverContext, loaderContext, imports)方法->this.load(loaderContext, resolved);

 ConfigDataEnvironmentContributors withProcessedImports(ConfigDataImporter importer, ConfigDataActivationContext activationContext) {
        ConfigDataEnvironmentContributor.ImportPhase importPhase = ImportPhase.get(activationContext);
        this.logger.trace(LogMessage.format("Processing imports for phase %s. %s", importPhase, activationContext != null ? activationContext : "no activation context"));
        ConfigDataEnvironmentContributors result = this;
        int processed = 0;

        while(true) {
            ConfigDataEnvironmentContributor contributor = this.getNextToProcess(result, activationContext, importPhase);
            if (contributor == null) {
                this.logger.trace(LogMessage.format("Processed imports for of %d contributors", processed));
                return result;
            }

            if (contributor.getKind() == Kind.UNBOUND_IMPORT) {
                Iterable<ConfigurationPropertySource> sources = Collections.singleton(contributor.getConfigurationPropertySource());
                PlaceholdersResolver placeholdersResolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(result, activationContext, true);
                Binder binder = new Binder(sources, placeholdersResolver, (ConversionService)null, (Consumer)null, (BindHandler)null);
                ConfigDataEnvironmentContributor bound = contributor.withBoundProperties(binder);
                result = new ConfigDataEnvironmentContributors(this.logger, this.bootstrapContext, result.getRoot().withReplacement(contributor, bound));
            } else {
                ConfigDataLocationResolverContext locationResolverContext = new ContributorConfigDataLocationResolverContext(result, contributor, activationContext);
                ConfigDataLoaderContext loaderContext = new ContributorDataLoaderContext(this);
                List<ConfigDataLocation> imports = contributor.getImports();
                this.logger.trace(LogMessage.format("Processing imports %s", imports));
                Map<ConfigDataResolutionResult, ConfigData> imported = importer.resolveAndLoad(activationContext, locationResolverContext, loaderContext, imports);
                this.logger.trace(LogMessage.of(() -> {
                    return this.getImportedMessage(imported.keySet());
                }));
                ConfigDataEnvironmentContributor contributorAndChildren = contributor.withChildren(importPhase, this.asContributors(imported));
                result = new ConfigDataEnvironmentContributors(this.logger, this.bootstrapContext, result.getRoot().withReplacement(contributor, contributorAndChildren));
                ++processed;
            }
        }
    }

在这里插入图片描述
到这里就差不多加载到配置文件了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/911752.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

轻松实现24小时无人直播带货,只需一款无人值守手机直播软件!

现在做线上运营&#xff0c;基本上就离不开短视频平台&#xff0c;想要做好短视频平台&#xff0c;就得弄懂如何在平台上进行直播。 今年以来&#xff0c;以专帮科技为首的一些科技公司研发的手机无人直播技术得到了快速发展&#xff0c;使得越来越多的企业和个人开始使用此类…

不溶性微粒该如何检测?液体粒子计数器

不溶性微粒是什么&#xff1f; 不溶性微粒系指可流动的、随机存在于静脉注射用药物中不溶于水的微小颗粒&#xff0c;通常采用光阻法(Light Obscuration Particles Count Test)和显微计数法(Microscopic Particles Count Test)进行检测。所谓静脉注射用药物中不溶于水的微小颗…

Go与Rust的对比与分析

Rust 和 Go 是两种现代语言&#xff0c;近年来获得了巨大的关注&#xff0c;每种语言都有自己独特的优势和权衡。在这篇文章中&#xff0c;我们将深入探讨 Rust 和 Go 之间的差异&#xff0c;重点关注性能、语言功能和其他关键因素&#xff0c;以帮助您针对您的开发需求做出明智…

VS13打开后菜单中没有“生成”这个选项

小白新手学开发之vs2013下载后没有生成菜单 今天下载了vs2013竟然发现程序在调试的时候&#xff0c;菜单上没有了生成这个选项&#xff01; 很苦恼&#xff0c;上网查了半天&#xff0c;也没有找到合适的方法&#xff0c;自己琢磨吧&#xff01; 不舍得把好不容易安装的这个v…

Workspace ONE 统一端点管理系统对 Windows 多用户的支持

自从二十多年前微软推出 Active Directory&#xff08;AD&#xff09;以来&#xff0c;用户就可以使用他们在 AD 中的任何账户登录到 Windows 域连接的 PC 上&#xff0c;而该 PC 将根据他们的需求量身定制。组策略对象&#xff08;Group policy objects, GPOs&#xff09;使这…

番茄(西红柿)叶病害识别(Python代码,pyTorch框架,深度卷积网络模型,很容易替换为其它模型,带有GUI识别界面)

代码运行要求&#xff1a;Torch>1.13.1即可 1.数据集介绍&#xff1a; 每一个文件夹里装有一类病害叶子的照片&#xff0c;一共10种类别&#xff0c;每种类别下有1100张照片 从第一类到第十类分别如下图所示 2.整体文件夹 data文件夹存放的是未被划分训练集和测试集的原…

HCIP实验之三层架构

目录 一、实验题目 二、实验思路 2.1 拓扑设计&#xff08;IP地址规划&#xff09; 2.2 实施 2.3 维护 2.4 升级 涉及知识点&#xff1a; 配置顺序: 三、实验步骤 3.1 建立eth-trunk 3.2 建立vlan 3.3 划入vlan 3.4 trunk干道 3.5 STP生成树协议 3.6 调整边缘接口…

解决vant组件 van-dialog造成的页面闪动问题

解决方案&#xff1a;该问题是因为van-dialog默认是scale&#xff0c;将这个属性改为fade即可

解决express引入express-session报错require(...) is not a function

我引入的版本是7.1.0 然后就报错 换成了6.1.0 就没事了 具体的还没有探究

Android开发基础知识总结(三)简单控件(上)

一.文本显示 考虑到结构样式相分离的思想&#xff0c;我们往往在XML中设置文本 <TextViewandroid:layout_width"342dp"android:layout_height"70dp"android:text"房价计算器"android:layout_gravity"center"android:textColor"…

OLED透明屏:如何选择合适的OLED透明屏供应商?定制、安装、生产

引言&#xff1a;OLED透明屏作为一种创新的显示技术&#xff0c;正逐渐占领市场并在各个行业中得到广泛应用。 在这篇文章中&#xff0c;尼伽将为您提供OLED透明屏的品牌排名、制造过程和安装要点的综合指南&#xff0c;结合相关调查数据和报告&#xff0c;详细介绍该技术的优…

Linux系统查看文件系统类型C代码

系统&#xff1a;VM Ubuntu 实现Linux系统下通过输入指定路径查看文件系统类型,MSDOS_SUPER_MAGIC&#xff0c;NTFS_SUPER_MAGIC和EXT4_SUPER_MAGIC这些宏定义并不是在sys/mount.h中定义的&#xff0c;它们实际上是在linux/magic.h头文件中定义的。不同系统下宏定义可能不一样&…

基于微信小程序的美食系统/基于微信小程序的美食网站/基于springboot的美食宣传平台

摘 要 在Internet高速发展的今天&#xff0c;我们生活的各个领域都涉及到计算机的应用&#xff0c;其中包括微信小程序美食天下的网络应用&#xff0c;在外国微信小程序美食天下已经是很普遍的方式&#xff0c;不过国内的美食天下可能还处于起步阶段。微信小程序美食天下具有在…

rook-ceph部署

宿主机&#xff1a; MacBook Pro&#xff08;Apple M2 Max&#xff09; VMware Fusion Player 版本 13.0.2 VM软硬件&#xff1a; ubuntu 22.04.2 4核 CPU&#xff0c;5G 内存&#xff0c;40G硬盘 *每台机器分配硬件资源很重要&#xff0c;可以适当超过宿主机的资源量&am…

【php】windows下php运行已有php web项目环境配置教程

php环境配置教程 php安装composer安装扩展安装redis扩展安装 composer install 本文操作系统使用的是win11&#xff0c;软件PhpStorm 2023.1 php安装 要安装的php版本可以在composer.json看到&#xff0c;下载安装对应版本 windows下载地址https://windows.php.net/download …

华为OD机试之报文重排序【Java源码】

题目描述 对报文进行重传和重排序是常用的可靠性机制&#xff0c;重传缓中区内有一定数量的子报文&#xff0c;每个子报文在原始报文中的顺序已知&#xff0c;现在需要恢复出原始报文。 输入描述 输入第一行为N&#xff0c;表示子报文的个数&#xff0c;0 &#xff1c;N ≤ …

海外聚合广告SDK功能比较

目前主流的广告变现平台数量繁多&#xff0c;开发者在选择第三方聚合广告平台时&#xff0c;如何判别&#xff1f;APP开发者有哪些指标来衡量如何选择聚合广告平台呢&#xff1f; 1、接入及运营成本 广告SDK是否容易接入&#xff1f;聚合平台操作是否简单易懂&#xff1f;广告…

AMBA总线协议(5)——AHB(三):猝发传输

一、前言 在之前的文章中我们详细讲述了关于AHB的基本操作流程&#xff0c;主机要先从仲裁器获得授权&#xff0c;然后进行总线的访问&#xff0c;这样可以避免总线冲突&#xff0c;获得授权后&#xff0c;主机给出地址和控制信号&#xff0c;从机根据自身情况进行响应&#xf…

峰会回顾第7期 | 视窗绘制技术演进和新趋势

本文转载自 OpenHarmony TSC 官方微信公众号《峰会回顾第7期 | 视窗绘制技术演进和新趋势》 演讲嘉宾 | 陈秋林 回顾整理 | 廖 涛 排版校对 | 李萍萍 嘉宾简介 陈秋林&#xff0c;华为终端OS资深架构师、菲尔兹Lab主任&#xff0c;长期从事终端OS研发&#xff0c;先后负责Lit…