此处分享如何通过maven快速构建一个Spring Boot项目,步骤如下:
1、创建一个maven项目
2、添加关键pom依赖
添加spring-boot-starter-parent、spring-boot-starter-web、spring-boot-starter-test等关键依赖,pom文件如下:
<?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>
<groupId>com.demo</groupId>
<artifactId>springboot02</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springboot02</name>
<!--此依赖表示当前pom文件从spring-boot-starter-parent继承下来,
在spring-boot-starter-parent中提供了很多默认的配置,
这些配置可以大大简化我们的开发。-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--此依赖引入了web模块开发需要的相关jar包。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--此依赖引入了springboot程序单元测试需要的相关jar包。-->
<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>
3、创建启动类
编写一个项目启动类,添加@SpringBootApplication
注解。
注意:启动类所在目录是Spring容器扫描创建Bean的根目录。
@SpringBootApplication
public class App {
public static void main( String[] args ) {
SpringApplication.run(App.class, args);
}
}
4、创建application.yml文件
在src/main目录下创建resources目录,并设为资源路径,在resources目录下创建application.yml文件,在application.yml文件做如下基本配置:
server:
port: 8090
servlet:
context-path: /
5、创建单元测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
@Test
public void test1() throws Exception{
}
}