SpringBoot通过main方法启动web项目实践

2025-08-18 22:50

本文主要是介绍SpringBoot通过main方法启动web项目实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring...

Spring Boot 通过 main 方法启动 Web 项目的过程涉及多个核心组件和自动化机制,下面从源码角度详细拆解:

1. 启动入口:SpringApplication.run()

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

SpringApplication.run() 是启动的核心入口,它主要完成以下工作:

2. SpringApplication初始化

// SpringApplication 构造函数核心逻辑
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    // 1. 设置资源加载器
    this.resourceLoader = resourceLoadphper;
    // 2. 校验并保存主配置类(即 @SpringBootApplication 标注的类)
    this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    // 3. 推断应用类型(REACTIVE、SERVLET、NONE)
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
   China编程 // 4. 加载并实例化 ApplicationContextInitializer
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    // 5. 加载并实例化 ApplicationListener
    setListeners((Collection) getSpringFactoriesInstancjses(ApplicationListener.class));
    // 6. 推断 main 方法所在类
    this.mainApplicationClass = deduceMainApplicationClass();
}

关键步骤:

  • 应用类型推断:通过检查类路径中是否存在 org.springframework.web.reactive.DispatcherHandler(REACTIVE)或 Javax.servlet.Servlet(SERVLET)来确定应用类型。
  • 初始化器(Initializer):从 META-INF/spring.factories 加载 ApplicationContextInitializer,用于在 ApplicationContext 刷新前自定义配置。
  • 监听器(Listener):加载 ApplicationListener,监听启动过程中的事件(如 ApplicationStartingEvent)。

3. run()方法核心流程

public ConfigurableApplicationContext run(String... args) {
    // 1. 计时和发布启动事件
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    
    // 2. 获取并启动监听器
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    
    try {
        // 3. 构建应用参数和环境配置
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        
        // 4. 创建并配置 ApplicationContext
        context = createApplicationContext();
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                new Class[] { ConfigurableApplicationContext.class }, context);
        
        // 5. 准备上下文(加载 Bean 定义)
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        // 6. 刷新上下文(核心启动逻辑)
        refreshContext(context);
        // 7. 刷新后的回调处理
        afterRefresh(context, applicationArguments);
        // 8. 发布应用就绪事件
        listeners.started(context);
        // 9. 执行 Runner(如 CommandLineRunner)
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }
    
    stopWatch.stop();
    if (this.logStartupInfo) {
        new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
    }
    // 10. 发布应用运行中事件
    listeners.running(context);
    return context;
}

4. 嵌入式 Web 服务器启动关键点

4.1refreshContext()方法触发服务器启动

private void refreshContext(ConfigurableApplicationContext context) {
    refresh(context);
    if (this.registerShutdownHook) {
        try {
            context.registerShutdownHook();
        }
        catch (AccessControlException ex) {
            // Not allowed in some environments.
        }
    }
}

protected void refresh(ConfigurableApplicationContext context) {
    // 调用 AbstractApplicationContext 的 refresh() 方法
    context.refresh();
}

4.2ServletWebServerApplicationContext的核心作用

对于 Web 应用,ApplicationContext 实际类型为 AnnotationConfigServletWebServerApplicationContext,它继承自 ServletWebServerApplicationContext,后者在 refresh() 过程中会:

// ServletWebServerApplicationContext 核心方法
@Override
protected void onRefresh() {
    super.onRefresh();
    try {
        // 创建并启动嵌入式 Web 服务器
        createWebServer();
    }
    catch (Throwable ex) {
        throw new ApplicationContextException("Unable to start web server", ex);
    }
}

private void createWebServer() {
    WebServer webServer = this.webServer;
    ServletContext servletContext = getServletContext();
    
    if (webServer == null && servletContext == null) {
        // 1. 获取 ServletWebServerFactory(如 TomcatServletWebServerFactory)
        ServletWebServerFactory factory = getWebServerFactory();
        // 2. 创建并配置 Web 服务器
        this.webServer = factory.getWebServer(getSelfInitializer());
    }
    else if (servletContext != null) {
        try {
            getSelfInitializer().onStartup(servletContext);
        }
        catch (ServletException ex) {
            throw new ApplicationContextException("Cannot initialize servlet context", ex);
        }
    }
    initPropertySources();
}

4.3ServletWebServerFactory实例化服务器

以 Tomcat 为例,TomcatServletWebServerFactorygetWebServer() 方法会:

@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
    // 1. 创建 Tomcat 实例
    Tomcat tomcat = new Tomcat();
    // 2. 配置服务器基本参数(端口、上下文路径等)
    File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
    tomcat.setBaseDir(baseDir.getAbsolutePath());
    Connector connector = new Connector(this.protocol);
    tomcat.getService().addConnector(connector);
    custwww.chinasem.cnomizeConnector(connector);
    tomcat.setConnector(connector);
    tomcat.getHost().setAutoDeploy(false);
    // 3. 配置 ServletContextInitializer(如 DispatcherServlet)
    prepareContext(tomcat.getHost(), initializers);
    // 4. 启动服务器
    return getTomcatWebServer(tomcat);
}

5. Spring MVC 组件自动配置

通过 WebMvcAutoConfiguration 自动配置核心组件:

  • DispatcherServlet:作为前端控制器,处理所有 HTTP 请求。
  • HandlerMapping:映射 URL 到具体的 Controller 方法。
  • ViewResolver:解析视图名称到实际视图。

关键代码(WebMvcAutoConfiguration):

@Bean
@Primary
@ConditionalOnMissingBean(DispatcherServlet.class)
public DispatcherServlet dispatcherServlet(WebMvcProperties properties) {
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setDispatchOptionsRequest(properties.isDispatchOptionsRequest());
    dispatcherServlet.setDispatchTraceRequest(properties.isDispatchTraceRequest());
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(properties.isThrowExceptionIfNoHandlerFound());
    dispatcherServlet.setPublishEvents(properties.isPublishRequestHandledEvents());
    dispatcherServlet.setEnableLoggingRequestDetails(properties.isLogRequestDetails());
    return dispatcherServlet;
}

6. 最终启动结果

  • 嵌入式服务器(如 Tomcat)启动并监听指定端口(默认 8080)。
  • DispatcherServlet 注册到 Servlet 容器,作为所有请求的入口。
  • Spring 上下文初始化完成,所有 Bean 已加载并可用。
  • ApplicationReadyEvent 发布,标志应用可处理外部请求。

总结:启动流程关键点

  1. SpringApplication 初始化:推断应用类型、加载初始化器和监听器。
  2. 环境配置:加载 application.properties 等配置源。
  3. ApplicationContext 创建:根据 Web 类型选择相应的上下文实现。
  4. 自动配置:基于依赖和条件注解,自动配置 Web 组件(如 DispatcherServlet)。
  5. 嵌入式服务器启动:通过 ServletWebServerFactory 创建并启动 Tomcat/Jetty。
  6. Spring MVC 初始化:配置请求映射、视图解析等核心组件。

通过这种机制,Spring Boot 实现了“零配置”启动 Web 项目的能力,开发者只需关注业务逻辑,无需手动处理服务器配置和组件装配。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程China编程(www.chinasem.cn)。

这篇关于SpringBoot通过main方法启动web项目实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1155800

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三