【SpringBoot记录】自动配置原理(1):依赖管理

 前言

        我们都知道SpringBoot能快速创建Spring应用,其核心优势就在于自动配置功能,它通过一系列的约定和内置的配置来减少开发者手动配置的工作。下面通过最简单的案例分析SpringBoot的功能特性,了解自动配置原理。

SpringBoot简单案例

根据Spring官网Getting Started,以下是一个简单的基于SpringBoot的web应用。通过浏览器请求/hello,响应Hello 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.test</groupId>
    <artifactId>boot-01-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>


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

</project>

启动类MainApplication

package com.test.boot;


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

/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {

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

Controller

package com.test.boot.controller;


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

@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String handle01(){
        return "Hello SpringBoot";
    }


}

运行测试

直接运行MainApplication中main方法。

分析

从上面案例可以看出,对比直接使用spring+springmvc的方式,最直接的区别是:

①依赖变少了,只需要引入一个父项目parent和一个spring-boot-starter-web依赖

②不需要写配置文件

③启动时不需要将应用部署到tomcat,也不需要在idea中配置tomcat后启动应用

整个SpringBoot案例制只引入了一个父项目和一个依赖,只能由此展开分析。

SpringBoot依赖管理

父项目

在maven中,父项目一般用作依赖管理:父项目中声明依赖,子项目继承父项目后,在使用同样依赖时无需写版本号。通过上述pom文件可以看出,父项目中一定有spring-boot-starter-web依赖。不妨看看父项目中都有什么。

spring-boot-starter-parent

点开父项目,发现它还继承了一个父项目spring-boot-dependencies,其余是一个properties、build等启动相关参数,部分代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
  </parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <packaging>pom</packaging>
  <name>spring-boot-starter-parent</name>
  <description>Parent pom providing dependency and plugin management for applications built with Maven</description>
  <properties>
    <java.version>1.8</java.version>
    <resource.delimiter>@</resource.delimiter>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

.....

</project>

spring-boot-dependencies

继续查看spring-boot-dependencies.pom,其中必定有spring-boot-starter-web依赖,同时还声明了其他有很多依赖,几乎包括了所有开发中常用的依赖。同时对各个依赖的版本号进行了声明,自动进行版本仲裁。这意味着我们在使用这些这些依赖时,只要继承了父项目,在使用其中声明的依赖时,就不用再去解决各个依赖间的版本兼容问题

当然,如果需要使用特定的依赖版本,也可以在子项目中重新声明版本号。

starter场景启动器

了解完父项目,再看spring-boot-starter-web中具体引入了哪些依赖。先看看官方对于starter的描述。

简单的说,starter是一组依赖。通过引入一个starter依赖,该场景需要的所有依赖都被声明了,简化了开发的配置工作。比如上述spring-boot-starter-web,web开发场景需要的常规依赖都在其中。

这里需要注意的是,官方starter命名规范是:spring-boot-starter-xxxx。

也有一些第三方的starter,一般以xxx-spring-boot-starter命名。

SpringBoot所有支持的场景和相关的starter,部分如下图。除此之外,我们也可自行定义starter,后续会介绍。

spring-boot-starter-web

查看spring-boot-starter-web中的依赖,发现其中包括了spring核心依赖、tomcat依赖、一些其他web开发场景需要的依赖。

小结

以上就是SpringBoot的依赖管理,后面将继续在此案例基础上介绍SpringBoot的自动配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值