掘金 后端 ( ) • 2022-11-24 13:40

本文正在参加「金石计划 . 瓜分6万现金大奖」

一.SpringBoot

1.1 Spring和SpringBoot

Spring:Spring是一个开源框架, 2003年兴起的一个轻量级的Java开发框架, 作者:Rod Johnson Spring是为了解决企业级应用开发的复杂性而创建的, 简化开发。

Spring Boot

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

SpringBoot最核心的东西:自动装配。

1.2 本阶段学习任务

image-20221115235031596

1.3 微服务架构

微服务就是一些协同工作小而自治的服务。

与传统的单一软件架构不同,微服务架构是为满足当前互联网后台服务的“高并发、高性能、高可用性三高要求”而创建的软件架构。

image-20221116000354056

image-20221116000632757

二.搭建一个SpringBoot程序

环境:

jdk 1.8

maven 3.6.1

SpringBoot :最新版

idea

SpringBoot官方网站:

https://spring.io/projects/spring-boot#overview

2.1 新建SpringBoot项目(官方)

1.在官方网站里点击进入Spring Initializr.

image-20221116001431339

2.project选择Maven,语言选择java

版本选择最新的2.7.5,Java选择8,在右边将Springweb添加进去,点击下载,就会得到一个标准的Maven项目。

image-20221116001659902

3.使用idea打开这个项目即可:

image-20221116002138978

2.2 正常创建SpringBoot项目

1.直接在idea里创建一个Spring项目即可,如下配置

image-20221116002452001

2.添加依赖Spring Web

image-20221116002521867

总结:俩种创建方式1.在SpringBoot官网下载后,导入idea开发。2.可以直接在idea里创建项目进行开发。

2.3 项目结构

SpringbootDemoApplication为项目的主入口。

application.properties是项目的核心配置文件。

image-20221116003647684

2.4 启动项目

运行主入口:就会启动项目

public class SpringbootDemoApplication {

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

}

image-20221116003929782

访问本地8080端口:

image-20221116003949151

说明项目搭建成功。

2.5 写一个接口HelloControlier

这个接口,接口就是hello。调用业务,接受前端的参数

@RestController
public class HelloControlier {
    @RequestMapping("/hello")
    public String hello(){
        return "hello,world上进小菜猪";
    }
}

所以我们访问http://localhost:8080/hello,就会进入这个接口。

返回如下:

image-20221116004737068

原理:自动装配!

2.6 原理

依赖:这里配置web依赖,比如tomact,dispatcherServlet,xml等等:

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

单元测试:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

打jar包依赖:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2.7 更改配置

到application.properties里,写端口号:

server.port=8081

重新启动项目:发现现在的端口号变成了8081!

image-20221116005834717

三.原理初探

自动配置原理

在文件里的pom.xml里:

1.核心依赖都在父工程里。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

2.启动器

就是一个Springboot的启动场景,像下面的spring-boot-starter-web,会帮我们自动的导入web环境的所有依赖。

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

3.项目入口文件SpringbootDemoApplication

SpringApplication类

run方法

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

SpringApplication

这个类做了下面4个事情:

1.推断应用的类型是普通的项目还是Web项目

2.查找并加载所有可用初始化器,设置到initializers属性中中

3.找出所有的应用程序监听器,设置到listeners属性中

4.推断并设置main方法的定义类,找到运行的主类

@SpringBootApplication标注这个类是一个springboot的应用

点进去查看源码:

@SpringBootConfiguration:springboot的配置

  • @Configuration :spring 配置类
    • @Component:也是一个组件类

@EnableAutoConfiguration:自动配置

  • @AutoConfigurationPackage:自动配置包
    • @Import({AutoConfigurationPackages.Registrar.class}):自动配置“包注册”
    • @Import({AutoConfigurationImportSelector.class})自动配置导入选择

获取所有的配置:

List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);

获取候选的配置:核心代码

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = new ArrayList(SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()));
    ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).forEach(configurations::add);
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
    return configurations;
}

对于SpringBoot的理解,俩点:

1.自动装配

2.run()方法