1,037
社区成员




这是我参加“朝闻道”知识分享大赛的第五篇文章。
这是我们创建好项目后,自动帮我们生成的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
parent
这parent有什么用呢,其实就是在其中定义了若干的依赖管理,我们继承它就能够避免出现多个依赖使用相同技术时出现版本冲突问题。
我们继承spring-boot-starter-parent,在其中定义了若干个依赖管理(也可以不使用继承采用引入的形式也能实现,使用aliyun创建的项目就是使用引用的方式进行的),根据这些依赖管理就能同一我们使用技术的版本了,我们在写坐标时也不需要写对应的版本了。(如果springboot没有整合相应的技术,那我们还是要自己带上版本号)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
starter
它就是一个包含了若干个坐标的pom管理文件,使得我们导入一个starter就相当于导入了多个坐标,它其中其实定义了当前项目使用的所有依赖坐标,帮助我们达到减少依赖配置的目的。
如下starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
其中就包括如下内容(这只是一部分举例,实际上更加多):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
<scope>compile</scope>
</dependency>
引导类
它是我们springBoot工程的入口,运行其中的main方法就能开启springboot的项目
//引导类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
//返回对象其实就是一个可配置的spring容器
ConfigurableApplicationContext app = SpringApplication.run(DemoApplication.class, args);
//在其中我们同样可以获得我们加载进容器的bean,它会扫描的只有引导类所在包及其子包,所以不是其中的bean是不会被加载的
demoController bean = app.getBean(demoController.class);
System.out.println(bean);
}
}
内嵌tomcat
spring-boot-starter-web在我们添加这个starter时它内部就帮我们配置好了一个内置的tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--在web环境的依赖中,通过这个标签去排除tomcat的依赖,排除了服务器的依赖自然要添加别的服务器-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加一个新的服务器叫做jetty,比tomcat更加轻量级,可扩展性更加强-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>