springboot启动原理

本文深入探讨SpringBoot的启动过程,从基本配置如添加依赖和启动类注解,到核心注解@SpringBootApplication的解析,再到关键的启动原理,包括构建SpringApplication对象及执行run方法。通过分析2.4.0版本的源码,揭示了SpringBoot如何一步步启动应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

SpringBoot启动的基本配置

添加依赖

添加启动类注解

SpringBoot启动类注解解析

@SpringBootApplication 注解

@SpringBootConfiguration

@EnableAutoConfiguration

@ComponentScan

SpringBoot启动原理

构建SpringApplication对象

执行构建SpringApplication对象run方法


springboot启动原理

springboot 2.4.0 启动源码分析

SpringBoot启动的基本配置

SpringBoot启动的配置主要有以下两个部分

添加依赖

最基本的springboot依赖只需要添加一个springboot启动器的依赖即可,如果是web服务则添加web的启动器

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 ​
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.4.0</version>
         <relativePath/>
     </parent>
 ​
     <groupId>org.example</groupId>
     <artifactId>springboot.demo</artifactId>
     <version>1.0-SNAPSHOT</version>
     <dependencies>
 <!--    如果不需要web服务则直接添加spring-boot-starter依赖即可不过我们一般都是需要web容器,所以直接添加spring-boot-starter-web依赖-->
 <!--        <dependency>-->
 <!--            <groupId>org.springframework.boot</groupId>-->
 <!--            <artifactId>spring-boot-starter</artifactId>-->
 <!--        </dependency>-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
     </dependencies>
     <properties>
         <maven.compiler.source>8</maven.compiler.source>
         <maven.compiler.target>8</maven.compiler.target>
     </properties>
 </project>

添加启动类注解

springboot启动的时候需要在启动类上面加上@SpringBootAppl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值