- 创建新项目
- 创建SpringBoot项目
- 基于Maven配置创建项目
- 选择SpringBoot插件,可以不选择直接
next
- 指定项目名和项目的本地路径
- 项目创建成功
- 项目内容和介绍
├── .gitignore // 自生成的[git 忽略文件](https://www.liaoxuefeng.com/wiki/896043488029600/900004590234208) ├── .idea // idea 的配置文件,注意项目提交时不要提交该文件夹,idea 生成项目时自动被 Git 忽略 ├── .mvn // maven 的包装文件,当项目部署到没有安装 maven 的机器时,可以通过可执行文件 mvnw 使用 maven 命令 └── wrapper // 有兴趣的朋友可以看下文件下的源码 │ ├── MavenWrapperDownloader.java │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── HELP.md // maven 的帮助文档,idea 生成项目时自动被 Git 忽略 ├── demo2.iml // 同 .idea 文件夹 ├── mvnw // 可执行文件,可以通过该文件执行 maven 相关命令 ├── mvnw.cmd // mvnw 文件的相关文件,辅助 mvnw 文件的执行 ├── pom.xml // maven 项目配置文件 └── src // 代码放置路径,maven 默认的包结构 ├── main │ ├── java │ │ └── com │ │ └── example │ │ └── demo2 │ │ └── Demo2Application.java // 项目启动类 │ └── resources │ └── application.yml // 空的 SpringBoot 配置文件,默认的是 .properties 配置文件,考虑到 .yml 文件可读性更好更简洁改为了 .yml 文件 └── test └── java └── com └── example └── demo2 └── Demo2ApplicationTests.java // 此处文件树使用 Mac 命令行插件 tree 生成 // 安装办法:brew install tree
- 代码介绍
Demo2Application.java
pom.xml
以上,使用IntelliJ idea
创建了一个无任何更改的SpringBoot
应用。
实际上,使用开发工具创建SpringBoot
应用是基于Spring Initializr封装的简化办法。有兴趣的朋友可以试着点链接过去试着使用SpringBoot
官方的生成项目办法(在百度翻译中,Initializer
才是正确拼写,但是Spring
官网的title
使用了Initializr
,咱也不懂为啥-。-)。
可能有的朋友会遇到如果没有翻墙不能使用Spring Initializr
创建项目,可以考虑使用maven
创建一个项目,然后通过pom
文件添加SpringBoot
依赖jar
,然后通过使用国内的maven
镜像下载相关依赖包(修改maven
镜像的步骤本章没有讲解,可以去查一下百度,有很多办法)。
以下为使用maven
创建SpringBoot
项目的步骤:
- 创建新项目(图略)
- 创建
maven
项目
- 提供
maven
项目的GroupId
和ArtifactId
- 设置项目名和创建路径
- 一个干净的
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sample.spring</groupId> <artifactId>demo_boot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo_boot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <!-- 通过父依赖控制所有 SpringBoot 依赖的版本 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!-- SpringBoot 程序启动依赖包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- SpringBoot 程序测试依赖包,内置了 Junit 等测试工具 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- SpringBoot 定义的 maven 插件,封装了一些 maven 启动或编译时的命令和参数 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- 修改
pom
文件后的idea
配置加载
- 用下面的内容替换新建项目的
- 创建
package
和主程序类- 根据
groupId
创建层级包
- 创建主程序类
package com.sample.spring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; // import org.springframework.context.annotation.ComponentScan; // 该注解指定 SpringBoot 注解扫描包。 // 默认的与启动类同级目录或文件都会被扫描,所以只需要在启动类同级创建包,就不需要添加该注解 // @ComponentScan("com.example.demo2.controller") // 该注解指定 SpringBoot 启动类,使用启动命令启动时将执行该类 @SpringBootApplication public class DemoApplication { /** * main 函数,程序的启动类 */ public static void main(String[] args) { // 传入程序启动类实例和启动参数 SpringApplication.run(DemoApplication.class, args); } }
- 创建测试包
package com.sample.spring; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { @Test public void contextLoads() { } }
- 根据
下节将介绍基于本节创建的应用修改为一个web
应用该怎么做。