Spring Boot学习笔记总结(一)

1. Spring Boot

1.1 什么是Spring Boot?


Spring的目的是为了解决企业级应用开发的复杂性而创建的,简化开发。
在这里插入图片描述


SpringBoot就是一个JavaWeb的开发框架。
在这里插入图片描述
在这里插入图片描述

1.2 SpringBoot 官方的一些解释


SpringBoot是整合Spring技术栈的快速开发脚手架。

SpringBoot适用条件(官方规定):

  • java8以上版本。
  • Maven3.3以上版本。

此外,不同版本的Springboot,可能还对java或Maven版本有要求。这个对于每个版本可以查看它的reference Doc参考文档。
在这里插入图片描述
参考文档里面有个Getting Started快速上手。
在这里插入图片描述
在Getting Start中找到System Requirements系统环境。
在这里插入图片描述


springboot可以做的一些事情:

  • Microservices(微服务架构):将多个功能拆分成多个服务。
  • Reactive:响应式。
  • Cloud:云开发,分布式架构。
  • Web apps:Web开发,ssm设计。
  • Serverless:无服务开发(函数式服务)
  • Event Driven:事件驱动。
  • Batch:批处理。

springboot的两个技术栈:

  • servlet栈和响应栈,就是两种技术流。
  • 平时学到的springmvc等技术属于servlet栈层次的。
    在这里插入图片描述

Springboot的优缺点:
在这里插入图片描述
在这里插入图片描述

2. 微服务架构

2.1 单体应用架构


all in one 单体应用架构:
在这里插入图片描述

2.2 微服务架构


架构效果图:
在这里插入图片描述

微服务是一种架构风格。一个应用拆分为一组小型服务。

  • 微服务之间使用的是轻量级的HTTP进行交互的
  • 每个微服务运行在自己的进程内,也就是可以独立部署和升级。
  • 微服务围绕业务功能拆分。
    在这里插入图片描述

2.3 分布式要解决的问题


在这里插入图片描述
实现分布式要解决的问题:
在这里插入图片描述

这就可以通过SpringCloud 和 SpringCloud DataFlow(数据流)来解决。


云原生了解。
在这里插入图片描述
在这里插入图片描述

3. 搭建第一个SpringBoot程序

3.1 在官方下载一个SpringBoot程序


官方提供了一个快速生成的网站,IDEA也集成了这个网站。

从官方生成一个springboot程序,去Spring Boot官方 , 找到Spring Initializr。来创建一个Spring Boot程序。

在这里插入图片描述

之后,就可以选择版本什么的生成一个SpringBoot项目:
在这里插入图片描述

3.2 在IDEA中,创建SpringBoot程序(推荐)


在这里插入图片描述

4. Maven 配置jdk1.8编译


配置Maven的一些信息,让maven自动用jdk1.8版本来进行项目编译。

  • 配置这个的目的就是为了防止后面创建maven项目出现各种乱七八糟的版本。
<mirrors>
     <mirror>
       <id>nexus-aliyun</id>
       <mirrorOf>central</mirrorOf>
       <name>Nexus aliyun</name>
       <url>http://maven.aliyun.com/nexus/content/groups/public</url>
     </mirror>
</mirrors>
 
<profiles>
     <profile>
          <id>jdk-1.8</id>
          <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
          </activation>
          <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
          </properties>
     </profile>
</profiles>

5. SpringBoot的大体结构


创建一个普通的maven项目。

将一个普通的maven项目一步步演变成SpringBoot项目:


第一步:配置Pom.xml文件:

<?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>

    <groupId>com.itholmes</groupId>
    <artifactId>boot-01-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--引入父工程依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
    </parent>
    <!--即插即用,需要导入什么就导入什么。-->
    <dependencies>
        <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>

第二步:创建MainApplication类,并且编写代码。
在这里插入图片描述

package com.itholmes.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//@SpringBootApplication注解作用:告诉SpringBoot,这是一个Springboot应用。
//@SpringBootApplication注解标注的类是主程序类。
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

第三步:就可以编写业务逻辑了。

  • 这里注意必须与主程序同级目录下的controller层才可以!
    在这里插入图片描述
package com.itholmes.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handler01(){
        return "Hello,Spring Boot 2!";
    }
}

就可以启动SpringBoot程序来检验了。


第三步:创建application.properties文件。

# 修改端口号
server.port=8888

第四步:可以将整个SpringBoot打成jar来使用。

  • 同样按照官方文档,配置pom的一些内容:
    在这里插入图片描述
    在这里插入图片描述

第五步:执行jar包。
在这里插入图片描述

6. SpringBoot banner在线生成工具


像开始的这些图标啥的,想要修改的话,可以通过SpringBoot banner(引文意思:横幅广告)工具来生成:
在这里插入图片描述

在这里插入图片描述

7. SpringBoot的 Pom.xml内容分析

7.1 父工程 parent 和 修改依赖版本


对于springboot的所依赖包的版本什么的,都是来源于父工程:

<!--
    父工程的作用:
        用来做依赖管理的。
-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.4</version>
</parent>

在spring-boot-starter-parent中,还有一个父工程spring-boot-dependencies:

<!--
	spring-boot-dependencies包含了很多springboot所依赖的包。
	也就是环境,为我们搭建好了。

	也就是所谓的依赖管理。
-->
<parent>
 	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-dependencies</artifactId>
  	<version>2.6.4</version>
</parent>

依赖管理的优点:

  • 开发导入starter场景启动器。
  • 无序关注版本号,自动版本仲裁机制。
  • 也可以修改版本号。

如果想要修改一些对应的jar包的版本,例如mysql.version想要改成8版本的。

  • 1.查看spring-boot-dependencies里面规定当前依赖的版本标签(用的key)。
  • 2.在当前项目pom.xml文件中,重写properties里面对应的标签,改成需要使用的版本。
<!--
    父工程的作用:
        用来做依赖管理的。
-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.4</version>
</parent>

<!--直接在pom文件中,定义版本,这里的版本标签要和,父工程对应相同!-->
<properties>
    <mysql.version>8.0.16</mysql.version>
</properties>

<!--即插即用,需要导入什么就导入什么。-->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

在这里插入图片描述


注意可能有些jar包,是非版本仲裁的。就是springboot的依赖管理里面,没有管理该jar包。这个时候不要忘记导入。

7.2 starter场景启动器


在官方的,每个springboot对应的参考文档下,的Using Spring Boot,查看starter。
在这里插入图片描述
在这里插入图片描述


官方指定的starter的名字:
在这里插入图片描述

官方给出了一些starter,即插即用,在哪个场景下,就是用那个starter。
在这里插入图片描述


也有第三方给出的starter,甚至自己创建的starter。命名方式一般如下:

  • *-spring-boot-starter
  • 以后看到*-spring-boot-starter这种形式的,那就是第三方为我们提供的简化开发的场景启动器。

所有场景启动器最底层的依赖:

  • 所有的starter启动器都会依赖spring-boot-starter。
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.6.4</version>
  <scope>compile</scope>
</dependency>

7.3 SpringBoot的自动配置


在这里插入图片描述

例如:在pom文件中,导入的spring-boot-starter-web就包含了tomcat,springmvc全套的依赖。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

8. SpringBoot 默认的包结构


默认的包结构:

  • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来。
  • 这也就是为什么我们定义的一些内容,必须在和主程序同级目录下的原因,这是一种约束。
  • 这样就不用我们再配置xml文件,来扫描包了。也体现了SpringBoot自动配置的特性。
    在这里插入图片描述

如果我们就想扫描,不和主程序在同一个包及其子包下的话,也是可以配置的。

  • 在@SpringBootApplication注解中,有一个参数变量scanBasePackages,可以配置。
  • 这里的scanBasePackages参数其实就是@SpringBootApplication注解就是被@ComponentScan注解修饰了。
package com.itholmes.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

//@SpringBootApplication注解的scanBasePackages可以修改扫描范围。
@SpringBootApplication(scanBasePackages="com.itholmes")
public class MainApplication {
    public static void main(String[] args) {
    	//这里的run就是IOC容器。给我们返回了IOC容器。
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
    }
}

@SpringBootApplication注解源码中,它还被三个注解修饰。

  • 直接使用这三个注解修饰主程序也是能达到相同的效果的。
    在这里插入图片描述

主程序:

package com.itholmes.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

//直接使用这三个也是可以达到效果的
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(basePackages="com.itholmes.boot")
public class MainApplication {
    public static void main(String[] args) {
		//这里的run就是IOC容器。给我们返回了IOC容器。
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

    }
}

9. SpringBoot的默认配置


SpringBoot都是有默认配置的,需要配置什么就可以在application.properties中配置。

  • application.properties配置文件的值最终都会被映射到一个类上面。这个类会在容器中创建对象。
    在这里插入图片描述

10. SpringBoot 按需加载(即插即用)


按需加载所有自动配置项:

  • 也就是需要那个starter就引入那个starter。
    在这里插入图片描述
  • SpringBoot所有的自动配置功能都在spring-boot-autoconfigure这个依赖包中。
    • spring-boot-autoconfigure中的内容不一定全部都生效(点开看有些事not found发红的),当我们引入我们需要的starter的时候,才会生效。
    • 例如:我引入了spring-boot-starter-batch,这时候spring-boot-autoconfigure的batch里面的一些类或注解就会生效。
      在这里插入图片描述
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

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

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

打赏作者

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

抵扣说明:

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

余额充值