Spring boot项目

mac2026-09-25  7

1.创建一个Spring boot项目

我是通过IDEA 的Spring Initializr创建的项目名为demo的项目 在pom.xml中加上

<packaging>jar</packaging>

打包 在\demo\target下生成demo-0.0.1-SNAPSHOT.jar,如下: 可以使用命令运行demo-0.0.1-SNAPSHOT.jar。

java -jar demo-0.0.1-SNAPSHOT.jar

以上是spring boot 项目的创建和运行过程,这是一个maven工程,为了理解这个项目,我从pom.xml入手去观察它。 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.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.iRain93</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <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</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2.观察一些问题

观察一下3块依赖的内容: 块1:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>

块2:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>

块3:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>

块4:

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

单独去掉块1,更新maven发现: 报错:

Cannot resolve org.springframework.boot:spring-boot-starter-test:unknown

去掉块4: 去掉块3: 可以在依赖库中找:spring-boot-starter-test-2.2.0.RELEASE.pom,打开看就知道怎么回事了, 以上的4块也就是4个项目,帮我们处理了一些事情,包括依赖包,只要使用它,就可以使用它包含的jar包,和功能,说白了,spring boot其实就是为了快速开发,规范了一些操作和jar包,提供使用

最新回复(0)