SpringBoot启动过程简介
SpringBoot应用程序的启动过程可以分为以下几个步骤:
加载应用程序上下文
扫描应用程序中的所有组件
自动配置应用程序环境
启动嵌入式Web服务器
加载应用程序上下文
一个包含 SpringBoot 应用程序所有组件的容器就是它的上下文。在启动过程中,SpringBoot 会加载并初始化这个容器。
这个步骤的源代码在SpringApplication
类中。具体来说,SpringApplication
类的run
方法是这个过程的入口点。在这个方法中,Spring Boot会通过调用createApplicationContext
方法来创建应用程序上下文。
下面是createApplicationContext
方法的源代码:
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会扫描应用程序中的所有组件并将它们注册到应用程序上下文中。
这个步骤的源代码在SpringApplication
类中的scan
方法中。具体来说,在这个方法中,SpringBoot 会创建一个SpringBootBeanDefinitionScanner
对象,并使用它来扫描应用程序中的所有组件。
下面是scan
方法的源代码:
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 会创建一个ClassPathScanningCandidateComponentProvider
对象,并使用它来扫描应用程序中的所有组件。这个对象会扫描指定包路径下的所有类,并将它们转换为 Spring 的 Bean 定义。这些 Bean 定义将被注册到应用程序上下文中。
自动配置应用程序环境
SpringBoot在前一步将应用程序中的所有组件注册到应用程序上下文中。SpringBoot会自动配置应用程序环境,其中包括数据源、事务管理器和JPA配置。
这个步骤的源代码在SpringApplication
类中的configureEnvironment
方法中。在这个方法中,Spring Boot会创建一个SpringApplicationRunListeners
对象,并使用它来配置应用程序环境。
下面是configureEnvironment
方法的源代码:
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 会创建一个ApplicationArguments
对象,并将其转换为一个命令行属性源。然后,它会调用listeners
中的environmentPrepared
方法来通知应用程序环境已经准备好了。随后,SpringBoot 会绑定属性源到应用程序环境中,并调用listeners
中的environmentPrepared
方法来通知应用程序环境已经准备好了。
启动嵌入式Web服务器
在前一步骤中,SpringBoot已自动完成了应用程序环境的配置。在这一步,SpringBoot将启动嵌入式Web服务器,以便应用程序提供Web服务。
这个步骤的源代码在SpringApplication
类中的run
方法中。具体来说,在这个方法中,SpringBoot 会根据应用程序类型(Servlet或Reactive)选择合适的嵌入式Web服务器,并使用它来启动应用程序。
下面是run
方法的源代码:
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 会使用一个StopWatch
对象来计算应用程序启动时间。然后,它会调用listeners
中的starting
方法来通知应用程序即将启动。接着,SpringBoot 会准备应用程序环境,并使用它来创建应用程序上下文。随后,SpringBoot 会调用listeners
中的started
方法来通知应用程序已经启动。最后,SpringBoot 会调用callRunners
方法来运行所有的CommandLineRunner
和ApplicationRunner
组件。
以上是SpringBoot启动流程是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

要解决Java应用程序中的平台特定问题,可以采取以下步骤:1.使用Java的System类查看系统属性以了解运行环境。2.利用File类或java.nio.file包处理文件路径。3.根据操作系统条件加载本地库。4.使用VisualVM或JProfiler优化跨平台性能。5.通过Docker容器化确保测试环境与生产环境一致。6.利用GitHubActions在多个平台上进行自动化测试。这些方法有助于有效地解决Java应用程序中的平台特定问题。

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1
功能强大的PHP集成开发环境