SpringBoot的原理分析和使用

SpringBoot

springboot简介

SpringBoot 对Spring的缺点进行的改善和优化,基于约定优于配置的思想,可以让开发人员不必在配置与逻辑业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发的效率,一定程度上缩短了项目周期。

SpringBoot特点

1. Spring Boot 是对微服务软件架构的实现 为基于Spring的开发提供更快的入门体验
2. 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求
3. 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等
4. SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式

springboot两大核心功能及与原理分析

【起步依赖】

起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能

SpringBoot的起步依赖:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>

web的起步依赖:
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

资源引用包括以下三种格式:
<resources>
        <resource>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>**/application*.yml</include>
                <include>**/application*.yaml</include>
                <include>**/application*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <excludes>
                <exclude>**/application*.yml</exclude>
                <exclude>**/application*.yaml</exclude>
                <exclude>**/application*.properties</exclude>
            </excludes>
        </resource>
</resources>

1.所有的springboot工程直接或间接继承spring-boot-starter-parent

2.spring-boot-starter-parent依赖spring-boot-dependencies

3.spring-boot-starter-dependencies中定义了 坐标版本 依赖管理 插件管理等

4.创建的SpringBoot工程则间接继承了spring-boot-starter-dependencies,并使用其配置

5.spring-boot-starter-web就是将web开发要使用的spring-web、spring-webmvc等坐标“打包”,这样我们的工程只要引入spring-boot-starter-web起步依赖的坐标就可以进行web开发了,体现依赖传递的作用。

【自动配置】

Spring Boot 的自动配置是一个运行时(更准确地说,是应用程序启动时)的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个。该过程是Spring自动完成的

查看源码:


1.按住 Ctrl 点击 SpringBootApplication,进入 SpringBootApplication 注解一探究竟
在这里插入图片描述
3.@EnableAutoConfiguration中有自动导入配置的类AutoConfigurationImporSelector
在这里插入图片描述
4.如下图,AutoConfigurationImportSelector的selectImports方法会调用getCandidateConfigurations
在这里插入图片描述
在这里插入图片描述
5.getCandidateConfigurations方法中的SpringFactoriesLoader.loadFactoryNames 会在spring-boot-test-autoconfig的META-INF下读取指定对应类名称列表(spring.factories)
在这里插入图片描述

6.查看spring.factories文件,存在大量的以 Configuration 为结尾的类名称,这些类就是存有自动配置信息的类,如果有关自动配置的配置信息类如果pom文件中有相关的坐标,则加载
在这里插入图片描述
7.尝试: 在自动配置的配置信息类中有配置信息的文件如ServletWebServerFactoryAutoConfiguration中配置了ServerProperties配置具体信息

应用

1. 创建Maven工程
2. 添加SpringBoot的起步依赖
	SpringBoot要求项目要继承SpringBoot的起步依赖spring-boot-starter-parent
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    	SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web    的启    动依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
3. 编写SpringBoot引导类(在基础包内)
	原因:通过SpringBoot提供的引导类起步SpringBoot才可以进行访问
	@SpringBootApplication:声明该类是一个SpringBoot启动类(引导类)具备多种功能
	SpringApplication.run(MySpringBootApplication.class)     代表运行SpringBoot的启动类,参数为SpringBoot启动类的字节码对象  

Spring Boot 热部署

1.pom文件添加坐标
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>
2.application.yml 配置
  spring:
      devtools:
        restart:
          # devtools 排除不需要检测的资源 和 增加额外需要监测的资源
          exclude: application.yml
          additional-paths: src/main/webapp
          # 是否重启,如果设置为false禁用,依旧会初始化重启类加载器,但它不会监控文件变化
          enabled: true
          # 触发器文件,只有在修改该文件的时候,才能触发工程重启
          #trigger-file: trigger-file
        livereload:
          # 内嵌的 LiveReload 服务器,可以在资源改变时,触发浏览器更新,禁用设置为false
          enabled: true
3. Ctrl + Shift+Alt+/ …runing 调出 Regiistry 窗口 …appruning勾选

如有不同见解,欢迎留言共同交流,博主看到后会在第一时间回复哦…

SpringBoot的运行原理可以分为以下几个方面: 1️⃣ 父依赖:SpringBoot项目的pom.xml文件中会引入一个父依赖,这个父依赖中包含了一些常用的依赖插件,简化了项目的配置构建过程。 2️⃣ starter场景启动器:SpringBoot提供了一系列的starter依赖,每个starter都包含了一组相关的依赖配置,可以方便地引入配置需要的功能。 3️⃣ 主启动类:SpringBoot项目的主启动类使用@SpringBootApplication注解进行标注,这个注解表示这是一个Spring Boot应用。在主启动类中,通过调用SpringApplication.run方法来启动Spring Boot应用。 4️⃣ spring.factories:Spring Boot使用spring.factories文件来配置自动装配的类。这个文件中定义了一些自动配置类的全限定名,当应用启动时,Spring Boot会根据这些配置来自动装配相应的功能。 5️⃣ SpringApplication.run分析SpringApplication.run方法是Spring Boot应用的入口,它会创建一个SpringApplication实例,并根据配置来启动Spring Boot应用。在启动过程中,会加载配置文件、创建Spring容器、执行自动装配等操作。 总结起来,SpringBoot原理可以概括为通过父依赖、starter场景启动器、主启动类、spring.factoriesSpringApplication.run方法来简化自动化Spring应用的配置启动过程。\[1\] \[2\] \[3\] #### 引用[.reference_title] - *1* *2* [SpringBoot运行原理分析](https://blog.youkuaiyun.com/gaowenhui2008/article/details/130456549)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Springboot工作原理详解](https://blog.youkuaiyun.com/huangtenglong/article/details/127862112)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Double@加贝

非常感谢家人们的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值