springboot干什么的_springboot初探---spring-boot-starter-web究竟干了啥

上一篇已经简单介绍了启动类的部分,这一篇主要讨论一下springboot引入的哪些依赖

我们都知道想用springboot做一个web应用,首先要做的是引入相关依赖,两步操作:

1、添加spring-boot-starter-parent

2、引入spring-boot-starter-web

至此所有关于web的依赖全部应用完毕

以前我们在用spring的时候,还需要添加很多依赖,包括spring-webmvc、spring-context、spring-bean等等,为什么到springboot就只需要这两个了呢?

现在来一探究竟

先从spring-boot-starter-parent开始,先贴一下源码

4.0.0

org.springframework.boot

spring-boot-dependencies

2.1.1.RELEASE

../../spring-boot-dependencies

spring-boot-starter-parent

pom

Spring Boot Starter Parent

Parent pom providing dependency and plugin management for applications

built with Maven

https://projects.spring.io/spring-boot/#/spring-boot-starter-parent

UTF-8

1.8

@

${java.version}

UTF-8

${java.version}

true

${basedir}/src/main/resources

**/application*.yml

**/application*.yaml

**/application*.properties

${basedir}/src/main/resources

**/application*.yml

**/application*.yaml

**/application*.properties

org.jetbrains.kotlin

kotlin-maven-plugin

${kotlin.version}

compile

compile

compile

test-compile

test-compile

test-compile

${java.version}

true

maven-compiler-plugin

true

maven-failsafe-plugin

integration-test

verify

${project.build.outputDirectory}

maven-jar-plugin

${start-class}

true

maven-war-plugin

${start-class}

true

org.codehaus.mojo

exec-maven-plugin

${start-class}

maven-resources-plugin

${resource.delimiter}

false

pl.project13.maven

git-commit-id-plugin

revision

true

yyyy-MM-dd'T'HH:mm:ssZ

true

${project.build.outputDirectory}/git.properties

org.springframework.boot

spring-boot-maven-plugin

repackage

repackage

${start-class}

maven-shade-plugin

package

shade

META-INF/spring.handlers

META-INF/spring.factories

META-INF/spring.schemas

${start-class}

org.springframework.boot

spring-boot-maven-plugin

2.1.1.RELEASE

true

true

*:*

META-INF/*.SF

META-INF/*.DSA

META-INF/*.RSA

org.eclipse.m2e

lifecycle-mapping

1.0.0

org.codehaus.mojo

flatten-maven-plugin

[1.0.0,)

flatten

org.apache.maven.plugins

maven-checkstyle-plugin

[3.0.0,)

check

首先看到的是spring-boot-starter-parent居然还有parent,这个先稍后再说。

然后看到的是标签,这里定义了哪些会被打包到jar里,指定了一堆plugin来帮助打包和编译

plugin有兴趣的可以研究,重点说一下配置文件的指定。

看到.yml、.yaml,.properties三种格式配置文件,当一个工程的resources下同时存在这三种文件时,优先加载的是.properties文件,为什么这么说呢,因为spring有个覆盖原则,后边的配置会覆盖掉前边,就算先加载.yml文件的内容,只要是相同的配置,依然会被后边的覆盖掉,但是不同的配置会被保留。

什么意思呢?举个例来说明一下,现在项目里有这样一个controller和三个配置文件

d5903ff4292c65baf4b23917005b1a24.png

ed46ea94fbe94e0d506e11e10046dcef.png

properties文件里下的内容是

b3572b202d2e45729af809d1f2a07b58.png

yaml文件里内容是

0b6f07b7e7b4f9df0a1a3c0895b8ff77.png

yml文件里的内容是

25e8a3b8f9b74f82b445b8029e3d98be.png

然而最终启动时用的端口是

d43fbe8ce9c6311882e596bd7a6569b4.png

访问接口打印的是

2ebf00552c78590609491ee1168b3066.png

说明properties>yml>yaml,另外不同路径也有不一样的优先级-file:/config/>-file:./>-classpath:/config/>-classpath:/

当然,有其他方式指定配置文件,有机会单独详细介绍

下面是spring-boot-starter-web的pom文件这里只是引入了springweb项目需要用的依赖,并没有什么好说的。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

4.0.0

org.springframework.boot

spring-boot-starters

2.1.9.RELEASE

org.springframework.boot

spring-boot-starter-web

2.1.9.RELEASE

Spring Boot Web Starter

Starter for building web, including RESTful, applications using Spring

MVC. Uses Tomcat as the default embedded container

https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-web

Pivotal Software, Inc.

https://spring.io

Apache License, Version 2.0

https://www.apache.org/licenses/LICENSE-2.0

Pivotal

info@pivotal.io

Pivotal Software, Inc.

https://www.spring.io

scm:git:git://github.com/spring-projects/spring-boot.git

scm:git:ssh://git@github.com/spring-projects/spring-boot.git

https://github.com/spring-projects/spring-boot

Github

https://github.com/spring-projects/spring-boot/issues

org.springframework.boot

spring-boot-starter

2.1.9.RELEASE

compile

org.springframework.boot

spring-boot-starter-json

2.1.9.RELEASE

compile

org.springframework.boot

spring-boot-starter-tomcat

2.1.9.RELEASE

compile

org.hibernate.validator

hibernate-validator

6.0.17.Final

compile

org.springframework

spring-web

5.1.10.RELEASE

compile

org.springframework

spring-webmvc

5.1.10.RELEASE

compile

至此就是spring-boot-start-web的全部内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值