一篇文章带你掌握主流办公框架——SpringBoot( 三 )


<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>SpringBoot起步依赖在简单介绍SpringBoot的项目开发之后,你是否有疑惑为什么SpringBoot能够省略如此多的信息来直接开发
其实这一切都是源于SpringBoot的依赖的直接创建,我们称之为起步依赖:

  • parent起步依赖继承
  • starter起步依赖继承
我们给出部分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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!----ctrl+左键 可以查看源码><!--Maven的继承机制,继承了spring-boot-starter-parent配置文件,再点开查看父类spring-boot-dependencies--><!--spring-boot-dependencies里包含了大量的properties,dependencyManagement,build可供选择使用--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version></parent><groupId>com.itheima</groupId><artifactId>springboot_01_quickstart</artifactId><version>0.0.1-SNAPSHOT</version><!--固定使用1.8JDK--><properties><java.version>1.8</java.version></properties><!--起步依赖,查看源码可以查看到关于SpringMvc的相关依赖,包括SpringMVC和Tomcat--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--起步依赖,查看源码可以查看到test的相关依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><!--打包插件,直接运行服务器--><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>总而言之,SpringBoot创建时自带的一系列起步依赖帮助我们简化了大量SSM的繁琐操作
我们再来详细介绍几个词语:
Starter:
  • SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
Parent:
  • 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,并非依赖),以达到减少冲突的目的
实际开发:
  • 使用任意坐标时,仅书写GAV中的G和A,不需要书写V
  • 如若发生坐标错误,再指定Version(小心版本冲突)
SpringBoot程序启动SpringBoot程序启动方法就是开启Application.java文件即可
package com.itheima;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}我们给出两个注意点:
  • SpringBoot在创建项目时,采用jar的打包方式
  • SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
SpringBoot切换服务器我们最后给出一个Maven使用技巧来切换服务器
SpringBoot中默认使用Tomcat服务器并安装了对应插件,
那么我们如果想切换服务器,只需要排除掉Tomcat插件,并添加新的插件即可
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version></parent><groupId>com.itheima</groupId><artifactId>springboot_01_quickstart</artifactId><version>0.0.1-SNAPSHOT</version><properties><java.version>1.8</java.version></properties><!--我们采用排除依赖的方法去除tomcat服务器--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><!--我们新添新的jetty服务器坐标即可--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

经验总结扩展阅读