springboot初探springbootstarterweb究竟干了啥

编程

上一篇已经简单介绍了启动类的部分,这一篇主要讨论一下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开始,先贴一下源码

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

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-dependencies</artifactId>

<version>2.1.1.RELEASE</version>

<relativePath>../../spring-boot-dependencies</relativePath>

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

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

<properties>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

<resource.delimiter>@</resource.delimiter>

<maven.compiler.source>${java.version}</maven.compiler.source>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.target>${java.version}</maven.compiler.target>

</properties>

<build>

<resources>

<resource>

<filtering>true</filtering>

<directory>${basedir}/src/main/resources</directory>

<includes>

<include>**/application*.yml</include>

<include>**/application*.yaml</include>

<include>**/application*.properties</include>

</includes>

</resource>

<resource>

<directory>${basedir}/src/main/resources</directory>

<excludes>

<exclude>**/application*.yml</exclude>

<exclude>**/application*.yaml</exclude>

<exclude>**/application*.properties</exclude>

</excludes>

</resource>

</resources>

<pluginManagement>

<plugins>

<plugin>

<groupId>org.jetbrains.kotlin</groupId>

<artifactId>kotlin-maven-plugin</artifactId>

<version>${kotlin.version}</version>

<executions>

<execution>

<id>compile</id>

<phase>compile</phase>

<goals>

<goal>compile</goal>

</goals>

</execution>

<execution>

<id>test-compile</id>

<phase>test-compile</phase>

<goals>

<goal>test-compile</goal>

</goals>

</execution>

</executions>

<configuration>

<jvmTarget>${java.version}</jvmTarget>

<javaParameters>true</javaParameters>

</configuration>

</plugin>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<parameters>true</parameters>

</configuration>

</plugin>

<plugin>

<artifactId>maven-failsafe-plugin</artifactId>

<executions>

<execution>

<goals>

<goal>integration-test</goal>

<goal>verify</goal>

</goals>

</execution>

</executions>

<configuration>

<classesDirectory>${project.build.outputDirectory}</classesDirectory>

</configuration>

</plugin>

<plugin>

<artifactId>maven-jar-plugin</artifactId>

<configuration>

<archive>

<manifest>

<mainClass>${start-class}</mainClass>

<addDefaultImplementationEntries>true</addDefaultImplementationEntries>

</manifest>

</archive>

</configuration>

</plugin>

<plugin>

<artifactId>maven-war-plugin</artifactId>

<configuration>

<archive>

<manifest>

<mainClass>${start-class}</mainClass>

<addDefaultImplementationEntries>true</addDefaultImplementationEntries>

</manifest>

</archive>

</configuration>

</plugin>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>exec-maven-plugin</artifactId>

<configuration>

<mainClass>${start-class}</mainClass>

</configuration>

</plugin>

<plugin>

<artifactId>maven-resources-plugin</artifactId>

<configuration>

<delimiters>

<delimiter>${resource.delimiter}</delimiter>

</delimiters>

<useDefaultDelimiters>false</useDefaultDelimiters>

</configuration>

</plugin>

<plugin>

<groupId>pl.project13.maven</groupId>

<artifactId>git-commit-id-plugin</artifactId>

<executions>

<execution>

<goals>

<goal>revision</goal>

</goals>

</execution>

</executions>

<configuration>

<verbose>true</verbose>

<dateFormat>yyyy-MM-dd"T"HH:mm:ssZ</dateFormat>

<generateGitPropertiesFile>true</generateGitPropertiesFile>

<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>

</configuration>

</plugin>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<executions>

<execution>

<id>repackage</id>

<goals>

<goal>repackage</goal>

</goals>

</execution>

</executions>

<configuration>

<mainClass>${start-class}</mainClass>

</configuration>

</plugin>

<plugin>

<artifactId>maven-shade-plugin</artifactId>

<executions>

<execution>

<phase>package</phase>

<goals>

<goal>shade</goal>

</goals>

<configuration>

<transformers>

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">

<resource>META-INF/spring.handlers</resource>

</transformer>

<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">

<resource>META-INF/spring.factories</resource>

</transformer>

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">

<resource>META-INF/spring.schemas</resource>

</transformer>

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>

<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

<mainClass>${start-class}</mainClass>

</transformer>

</transformers>

</configuration>

</execution>

</executions>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<version>2.1.1.RELEASE</version>

</dependency>

</dependencies>

<configuration>

<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>

<createDependencyReducedPom>true</createDependencyReducedPom>

<filters>

<filter>

<artifact>*:*</artifact>

<excludes>

<exclude>META-INF/*.SF</exclude>

<exclude>META-INF/*.DSA</exclude>

<exclude>META-INF/*.RSA</exclude>

</excludes>

</filter>

</filters>

</configuration>

</plugin>

<plugin>

<groupId>org.eclipse.m2e</groupId>

<artifactId>lifecycle-mapping</artifactId>

<version>1.0.0</version>

<configuration>

<lifecycleMappingMetadata>

<pluginExecutions>

<pluginExecution>

<pluginExecutionFilter>

<groupId>org.codehaus.mojo</groupId>

<artifactId>flatten-maven-plugin</artifactId>

<versionRange>[1.0.0,)</versionRange>

<goals>

<goal>flatten</goal>

</goals>

</pluginExecutionFilter>

<action>

<ignore/>

</action>

</pluginExecution>

<pluginExecution>

<pluginExecutionFilter>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-checkstyle-plugin</artifactId>

<versionRange>[3.0.0,)</versionRange>

<goals>

<goal>check</goal>

</goals>

</pluginExecutionFilter>

<action>

<ignore/>

</action>

</pluginExecution>

</pluginExecutions>

</lifecycleMappingMetadata>

</configuration>

</plugin>

</plugins>

</pluginManagement>

</build>

</project>

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

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

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

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

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

properties文件里下的内容是

yaml文件里内容是

yml文件里的内容是

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

访问接口打印的是

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

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

 

 

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

<?xml version="1.0" encoding="UTF-8"?>

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"

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

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starters</artifactId>

<version>2.1.9.RELEASE</version>

</parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<version>2.1.9.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>

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

<organization>

<name>Pivotal Software, Inc.</name>

<url>https://spring.io</url>

</organization>

<licenses>

<license>

<name>Apache License, Version 2.0</name>

<url>https://www.apache.org/licenses/LICENSE-2.0</url>

</license>

</licenses>

<developers>

<developer>

<name>Pivotal</name>

<email>info@pivotal.io</email>

<organization>Pivotal Software, Inc.</organization>

<organizationUrl>https://www.spring.io</organizationUrl>

</developer>

</developers>

<scm>

<connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>

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

<url>https://github.com/spring-projects/spring-boot</url>

</scm>

<issueManagement>

<system>Github</system>

<url>https://github.com/spring-projects/spring-boot/issues</url>

</issueManagement>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

<version>2.1.9.RELEASE</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-json</artifactId>

<version>2.1.9.RELEASE</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<version>2.1.9.RELEASE</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>org.hibernate.validator</groupId>

<artifactId>hibernate-validator</artifactId>

<version>6.0.17.Final</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>5.1.10.RELEASE</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>5.1.10.RELEASE</version>

<scope>compile</scope>

</dependency>

</dependencies>

</project>

 

 

 

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

 

 

以上是 springboot初探springbootstarterweb究竟干了啥 的全部内容, 来源链接: utcz.com/z/510954.html

回到顶部