SpringBoot启动流程是什么

SpringBoot启动过程简介SpringBoot应用程序的启动过程可以分为以下几个步骤:
加载应用程序上下文一个包含 SpringBoot 应用程序所有组件的容器就是它的上下文。在启动过程中,SpringBoot 会加载并初始化这个容器。 这个步骤的源代码在 下面是 protected ConfigurableApplicationContext createApplicationContext() { Class<?> contextClass = this.applicationContextClass; if (contextClass == null) { try { switch (this.webApplicationType) { case SERVLET: contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break; default: contextClass = Class.forName(DEFAULT_CONTEXT_CLASS); } } catch (ClassNotFoundException ex) { throw new IllegalStateException( "Unable to create a default ApplicationContext, " + "please specify an ApplicationContextClass", ex); } } return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); } 在这个方法中,SpringBoot 会根据应用程序类型(Servlet或Reactive)选择合适的上下文类。接着,使用 Java 反射机制来实例化该类并返回一个可配置的应用程序上下文对象。 扫描应用程序中的所有组件在上一步中,SpringBoot创建了应用程序上下文。在此阶段,SpringBoot会扫描应用程序中的所有组件并将它们注册到应用程序上下文中。 这个步骤的源代码在 下面是 private void scan(String... basePackages) { if (ObjectUtils.isEmpty(basePackages)) { return; } ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider( this.includeFilters, this.excludeFilters, this.resourceLoader); scanner.setResourceLoader(this.resourceLoader); scanner.setEnvironment(this.environment); scanner.setIncludeAnnotationConfig(this.useAnnotatedConfig); scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) { @Override protected boolean matchClassName(String className) { return getExcludeClassNames().contains(className); } }); for (String basePackage : basePackages) { scanner.findCandidateComponents(basePackage).forEach(this.componentDefinitions::add); } } 在这个方法中,SpringBoot 会创建一个 自动配置应用程序环境SpringBoot在前一步将应用程序中的所有组件注册到应用程序上下文中。SpringBoot会自动配置应用程序环境,其中包括数据源、事务管理器和JPA配置。 这个步骤的源代码在 下面是 private void configureEnvironment(ConfigurableEnvironment environment, String[] args) { if (this.addCommandLineProperties) { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); environment.getPropertySources().addLast(new CommandLinePropertySource(applicationArguments)); } this.listeners.environmentPrepared(environment); if (this.logStartupInfo) { this.logStartupInfo(environment); } ConfigurationPropertySources.attach(environment); Binder.get(environment).bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(this.sources)); if (!this.isCustomEnvironment) { EnvironmentConverter.configureEnvironment(environment, this.deduceEnvironmentClass()); } this.listeners.environmentPrepared(environment); } 在这个方法中,SpringBoot 会创建一个 启动嵌入式Web服务器在前一步骤中,SpringBoot已自动完成了应用程序环境的配置。在这一步,SpringBoot将启动嵌入式Web服务器,以便应用程序提供Web服务。 这个步骤的源代码在 下面是 public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } listeners.started(context); callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } return context; } 在这个方法中,SpringBoot 会使用一个 |
相关内容
win10多任务按键怎么设置在底部详细方法
win10多任务按键怎么设置在底部详细方法,按键,任务,在使用电脑...
学习写字楼新选择6000元主流配置
学习写字楼新选择6000元主流配置,,这种配置需要考虑双核心的办...
酷睿I7 配置
酷睿I7 配置,配置,玩家国度啦华硕 Rampage II Extreme(3800元)如...
提高3A四核羿龙II游戏配置的性能
提高3A四核羿龙II游戏配置的性能,,以节能环保为主题的IT产业,目...
opporeno8参数配置及价格
opporeno8参数配置及价格,面部,亿元,Oppo的荣誉2020年1月4日,...
设置里程碑|设置里程碑的方法有哪些
设置里程碑|设置里程碑的方法有哪些,,1. 设置里程碑的方法有哪...
查看配置:酷睿i3530集展示办公平台
查看配置:酷睿i3530集展示办公平台,,由于时间和精力的关系,我们...
3500元超额值学生娱乐结构的优化配置
3500元超额值学生娱乐结构的优化配置,,作为一个DIY的主流用户...
高档万元i7//GTX460组装X58电脑配置推荐
高档万元i7//GTX460组装X58电脑配置推荐,,GTX460赢得了良好的...
Outlook的PST文件损坏的修复方法
Outlook的PST文件损坏的修复方法,文件, ...
手提电脑手写怎么设置|电脑手写设置方法
手提电脑手写怎么设置|电脑手写设置方法,,电脑手写设置方法看...
打印机常见故障排除方法HP5000打印机为例
打印机常见故障排除方法HP5000打印机为例,,HP 50005100打印机...
电脑小白配置,求助各路装机大神指点?
电脑小白配置,求助各路装机大神指点?,小白,大神,各路,关于你这个...
2200元平台经济实用四核APU主机配置建议
2200元平台经济实用四核APU主机配置建议,,英特尔和平台间的较...
个人自配好的电脑|自己家用电脑配置
个人自配好的电脑|自己家用电脑配置,,1. 自己家用电脑配置CPU:...