1 依赖导入
<?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.springboot</groupId>
<artifactId>springboot-03</artifactId>
<version>1.0-SNAPSHOT</version>
<!--
说明:
Spring boot的项目必须要将parent设置为spring boot的parent,其中包含大量默认的配置
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<!--添加web支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2 应用
/*
代码说明:
1、@SpringBootApplication:SpringBoot的核心注解,负责开启自动配置
2、@Configuration:这是一个配置Spring的配置类;
3、@Controller:标明这是一个SpringMVC的Controller控制器;
4、main方法:在main方法中启动一个应用,即:应用的入口
*/
@Controller
@SpringBootApplication
@Configuration
public class HelloApplication {
@RequestMapping("hello")
@ResponseBody
public String hello() {
return "hello world!";
}
public static void main(String[] args) {
// 运行的类必须包含@SpringBootApplication
SpringApplication.run(HelloApplication.class, args);
}
}
3 启动
4 测试
http://127.0.0.1:8080/hello
5 项目打包
5.1 引入
<build>
<plugins>
<!--将应用打包成一个可执行jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5.2 打包,运行
-
将项目打包,切换到打包目录,打开命令窗口,执行:
java -jar springboot-03-1.0-SNAPSHOT.jar
6 分析
6.1 父项目:spring-boot-starter-parent
<!--说明:该父依赖是SpringBoot项目的必须依赖,是所有spring-boot-starter的父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
spring-boot-starter-parent的父依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
spring-boot-dependencies:
真正管理SpringBoot的所有依赖,以后导入其包含的依赖无需写版本号
6.2 导入依赖:spring-boot-starter-web
spring-boot-starter-web是SpringBoot的场景启动器,导入web模块正常运行所依赖的jar,其导入的依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--Tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
7 其他场景启动器
7.1 SpringBoot将所有功能场景抽取成相应的starts(启动器),只要在项目中引入这些starts,相关所有依赖都会被引入
7.2 详见SpringBoot官网文档
https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/html/using-boot-build-systems.html#using-boot-starter