SpringBoot 随着动态语言的流行,为解决 java 开发繁重的配置,低下的开发效率,复杂的部署以及第三方技术集成难度大而诞生。它使用习惯优于配置的理念能够让项目快速的运行,相较于传统的ssm、ssh项目而言,少了许多麻烦的配置文件。
SpringBoot 可以以jar包的方式独立运行,并且在其内部内嵌了 tomcat、jetty 等容器,无须以 war 包形式部署项目。提供了一系列的 starter pom 来简化 Maven 的依赖加载。
IDEA 搭建 SpringBoot 项目
- File => New => Project
这里选择 Spring Initializr
- 填写项目有关信息
- 选择 SpringBoot 提供的 starter 依赖,根据项目需要勾选
- 项目存放路径
这样一个 SpringBoot 项目就创建成功了,默认的 SpringBoot 项目目录如下,跟 Maven 项目类似:
- SpringbootDemoApplicaiton : 整个项目的启动类,通过运行其中的 main 方法快速启动项目
- application.properties :整个项目的配置文件,SpringBoot基于习惯优于配置理念,相较ssm等项目少了许多配置,除了可以使用properties格式,也可以使用 YAML 格式
- templates :项目默认的页面存放文件夹
- static :项目默认静态资源存放文件夹
默认的 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.ronin</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>springboot-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>
SpringBoot 项目都需要添加一个父级依赖,spring-boot-starter-parent 是一个特殊的 starter,是 spring 官方提供的,里面包含了众多第三方技术的jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
SpringBoot 的编译插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>