一.、概述
使用 Spring Boot 可以很容易地创建出能直接运行的独立的、生产级别的基于 Spring 的应用。
二、快速入门
<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.0modelVersion>
<groupId>cn.iocodergroupId>
<artifactId>demo01artifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.2.RELEASEversion>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
在 Spring Boot 项目中,约定通过 application.yaml 配置文件,进行 Spring Boot 自动配置的 Bean 的自定义。在 resource 目录下,创建 application.yaml 配置文件。
server:
port: 8080 # 内嵌的 Tomcat 端口号。默认值为 8080。
通过 server.port 配置项,设置稍后启动的内嵌 Tomcat 端口为 8080 端口。
创建 DemoController 类,提供一个简单的 HTTP API。
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/echo")
public String echo() {
return "echo";
}
}
创建 Application 类,提供 Spring Boot 应用的启动类。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在类上,添加 @SpringBootApplication 注解,声明是一个 Spring Boot 应用。通过该注解,可以带来 Spring Boot 自动配置等等功能。
在 #main(String[] args) 方法中,通过 SpringApplication#run(Class> primarySource, String… args) 方法,启动 Spring Boot 应用。
// Spring Boot 自带 Banner
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
// 启动 Java 进程使用的 PID 进程号
2020-02-08 15:38:25.724 INFO 10645 --- [ main] cn.iocoder.demo01.Application : Starting Application on MacBook-Pro-8 with PID 10645 (/Users/yunai/Java/demo01/target/classes started by yunai in /Users/yunai/Java/demo01)
// Spring Boot Profile 机制,暂时可以忽略
2020-02-08 15:38:25.727 INFO 10645 --- [ main] cn.iocoder.demo01.Application : No active profile set, falling back to default profiles: default
// 内嵌 Tomcat 启动
2020-02-08 15:38:26.503 INFO 10645 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-02-08 15:38:26.510 INFO 10645 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-02-08 15:38:26.510 INFO 10645 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-02-08 15:38:26.561 INFO 10645 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-02-08 15:38:26.561 INFO 10645 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 799 ms
2020-02-08 15:38:26.693 INFO 10645 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-08 15:38:26.839 INFO 10645 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-08 15:38:26.842 INFO 10645 --- [ main] cn.iocoder.demo01.Application : Started Application in 1.396 seconds (JVM running for 1.955)
// SpringMVC DispatcherServlet 初始化
2020-02-08 15:38:44.992 INFO 10645 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-02-08 15:38:44.992 INFO 10645 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-02-08 15:38:44.996 INFO 10645 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
// ... 暂时可以忽略
2020-02-08 15:39:37.113 INFO 10645 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
使用浏览器,访问 http://127.0.0.1:8080/demo/echo 接口,返回结果为 “echo”。
三、Spring Initializr
Spring Initializr可以通过使用 Spring Initializr工具,快速生成一个 Spring Boot 项目。
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.4.RELEASEversion>
<relativePath/>
parent>
<groupId>cn.iocodergroupId>
<artifactId>demo02artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo02name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
配置文件 application.properties 内容为空。
注意,Spring Boot 支持 YAML、PROPERTIES、JSON 等等格式的配置格式
@SpringBootApplication
public class Demo02Application {
public static void main(String[] args) {
SpringApplication.run(Demo02Application.class, args);
}
}
@SpringBootTest
class Demo02ApplicationTests {
@Test
void contextLoads() {
}
}
四、IDEA x Spring Initializr
IDEA 内置了 Spring Boot 插件,提供了对 Spring Initializr 集成。