spring区别

传统的 Spring 项目想要运行,不仅需要导入各种依赖,还要对各种XML 配置文件进行配置,十分繁琐,但 Spring Boot 项目在创建完成后,即使不编写任何代码,不进行任何配置也能够直接运行,这都要归功于Spring Boot 的 starter 机制。
starter

Spring Boot 将日常企业应用研发中的各种场景都抽取出来,做成一个个的 starter(启动器),starter 中整合了该场景下各种可能用到的依赖,用户 只需要在 Maven 中引入 starter 依赖,SpringBoot 就能自动扫描到要加载 的信息并启动相应的默认配置。starter 提供了大量的自动配置,让用户摆 脱了处理各种依赖和配置的困扰。所有这些 starter 都遵循着约定成俗的默 认配置,并允许用户调整这些配置,即遵循“约定大于配置”的原则。 并不是所有的 starter 都是由 Spring Boot 官方提供的,也有部分 starter 是第三方技术厂商提供的,例如 druid-spring-boot-starter 和 mybatis-spring-boot-starter 等等。 以 spring-boot-starter-web 为例,它能够为提供 Web 开发场景所需要的 几乎所有依赖,因此在使用 Spring Boot 开发 Web 项目时,只需要引入该 Starter 即可,而不需要额外导入 Web 服务器和其他的 Web 依赖。 在pom.xml中引入 spring-boot-starter-web,如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache/POM/4.0.0"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache/POM/4.0.0
https://maven.apache/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--SpringBoot父项目依赖管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starterparent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository
-->
</parent>
<groupId>com.by</groupId>
<artifactId>sdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sdemo</name>
<description>sdemo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--导入 spring-boot-starter-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starterweb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-startertest</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-mavenplugin</artifactId>
</plugin>
</plugins>
</build>
</project>

在该项目中执行以下 mvn 命令查看器依赖树。mvn dependency:tree

 从以上结果中,我们可以看到 Spring Boot 导入了 springframework、
logging、jackson 以及 Tomcat 等依赖,而这些正是我们在开发 Web 项目
时所需要的。
您可能会发现一个问题,即在以上 pom.xml 的配置中,引入依赖 springboot-starter-web 时,并没有指明其版本(version),但在依赖树中,我
们却看到所有的依赖都具有版本信息,那么这些版本信息是在哪里控制的
呢?其实,这些版本信息是由 spring-boot-starter-parent(版本仲裁中心) 统一控制的。

更多推荐

初识Spring Boot Starter