对开发导入依赖的粗浅理解-spring-boot-starter-web

对开发导入依赖的粗浅理解-spring-boot-starter-web

学习自用:本文意在解决在引入依赖时存在的疑惑
目的\达成效果:粗浅理解到所引入依赖的作用,以便抽取某些公共模块

1.spring-boot-starter-web

下面展示`spring-boot-starter-web的pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starters</artifactId>
    <version>2.0.5.RELEASE</version>
  </parent>
  
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.0.5.RELEASE</version>
  <name>Spring Boot Web Starter</name>
  
  <description>Starter for building web, including RESTful, applications using Spring
		MVC. Uses Tomcat as the default embedded container</description>

重点需要关注2个部分:
1.description部分:
可以看到,对于spring-boot-starter-web依赖的描述是这样的:
它是作为搭建web的启动器
包括三个要点:
a.Restful
b.SpringMVC的组件
c.tomcat作为默认的嵌入式容器

这意味着:
Restful : 在开发过程中,可以采用restful风格编写代码
SpringMVC的组件:
为什么我们能够在类上controller注解就将这个类变成一个控制器,这就是因为有SpringMVC组件的支持
tomcat作为嵌入式容器:后期深入学习补充

2.parent部分—spring-boot-starters
进入spring-boot-starters的pom文件

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath>../spring-boot-parent</relativePath>
  </parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starters</artifactId>
  <version>2.0.5.RELEASE</version>
  <packaging>pom</packaging>
  <name>Spring Boot Starters</name>
  <description>Spring Boot Starters</description>

关于spring-boot-starters的description(描述)很简单,就是spring-boot-starters,这里顾名思义地去理解,它就是springboot的一个启动器,在这个启动器的启动前提下,我们能够去启动web依赖下的东西

再进入spring-boot-starters的parent:spring-boot-parent

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath>../spring-boot-dependencies</relativePath>
  </parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-parent</artifactId>
  <version>2.0.5.RELEASE</version>
  <packaging>pom</packaging>
  <name>Spring Boot Parent</name>
  <description>Spring Boot Parent</description>

我的理解:springboot的starter启动器不能独自启动,也需要环境支撑,而springboot-parent就提供了springboot-starter的启动环境

再进入Spring Boot Paren的parent:spring-boot-dependencies

<modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.0.5.RELEASE</version>
    <packaging>pom</packaging>
    <name>Spring Boot Dependencies</name>
    <description>Spring Boot Dependencies</description>

可以看到,在spring-boot-dependencies的pom文件不再存在parent标签

继续往下看:
我们发现,在这里有大量的properties配置和dependencies依赖
篇幅有限,下面截取片段:

 <properties>
        <activemq.version>5.15.6</activemq.version>
        <antlr2.version>2.7.7</antlr2.version>
        <appengine-sdk.version>1.9.64</appengine-sdk.version>
        <artemis.version>2.4.0</artemis.version>
        <aspectj.version>1.8.13</aspectj.version>
        <assertj.version>3.9.1</assertj.version>
        <atomikos.version>4.0.6</atomikos.version>
        <bitronix.version>2.1.4</bitronix.version>
       ------此处省略大量------
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>2.0.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>2.0.5.RELEASE</version>
            </dependency>
     ----------此处省略大量---------

几个关键点:
1.在properties中存在大量的版本控制的配置信息:
解释:
这也就是为什么有时候当我们引入了spring-boot-starter-parent或者是spring-boot-parent依赖时,我们可以在引入如数据库mysql的依赖时,可以不用去配置数据库的版本信息,因为在上述两个依赖中已经对配置信息进行了管理。

2.在spring-boot-dependencies中依赖并非全部启用的
解释:

一、在spring-boot-dependencies的pom文件看到,有相当一部分的组件是红色的,这意味着它们并非自动引入的,只要当我们需要引入时才引入
二、在spring-boot-dependencies的pom文件中也存在一部分我们没有手动引入便自动开启的依赖,我的理解是它们是实现spring-boot的基础功能所必须的依赖;当我们引入springboot后,它们就处于默认开启状态,以便我们进行springboot开发

3.spring-boot-dependencies的作用
解释:

spring-boot-dependencies类似于一个一“依赖包的注册和管理中心”,它管理了大量的依赖和其默认的版本信息,如果你在引入spring-boot-dependencies中存在的jar包并且不携带其版本信息,那么就以spring-boot-dependencies中的默认版本信息为准,如果你在dependency中声明了版本信息,则以你所声明的版本信息为准

总结:

什么时候我们需要引入spring-boot-web包?
当我们进行springMVC架构开发,更直白地说,如果想要在类上注入controller、service等等、或者想要用GetMapping的restful风格注解,就需要引入spring-boot-starter-web包。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值