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勾选

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Lonestar@孤星

我这么欠揍,你来打赏我啊!

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

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

打赏作者

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

抵扣说明:

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

余额充值