Spring Boot 概述
Spring Boot(英文中是“引导”的意思),是用来简化Spring应用的搭建到开发的过程。应用开箱即用,只要通过 “just run”(可能是 java -jar 或 tomcat 或 maven插件run 或 shell脚本),就可以启动项目。二者,Spring Boot 只要很少的Spring配置文件(例如那些xml,property)。
因为“习惯优先于配置”的原则,使得Spring Boot在快速开发应用和微服务架构实践中得到广泛应用。
Spring Boot 开发流程
引入常用的依赖
spring-boot-starter-parent(必须):
父级依赖,所有 Spring Boot 组件的基础依赖包。指明这是一个Spring Boot项目。spring-boot-starter- parent 是一个特殊的starter,它用来提供相关的Maven默认依赖。使用它之后,常用的包依赖可以省去version标签。
spring-boot-starter @SpringBootApplication 启动依赖
spring-boot-starter-web 提供 Web 应用支持
spring-boot-starter-thymeleaf 提供 thymeleaf 模板引擎的支持
spring-boot-maven-plugin 提供 Spring Boot 应用打包的功能
创建 Maven 工程,构建项目结构。
配置 pom.xml,引用各种 starter 启动器简化配置。
配置运行参数。
编码与测试。
打包与独立运行
Spring Boot的目录结构与常用配置项
目录结构:
/src/main | 项目根目录 |
---|---|
/java | Java 源代码目录 |
/resources | 资源目录 |
/resources/static | 静态资源目录 |
/resources/templates | 表示层动态模板页面目录 |
/resources/application.properties | Spring Boot 配置文件 |
/test | 测试文件目录 |
WEB常用配置项:
WEB常用配置项 | 默认值 | 说明 |
---|---|---|
debug | false | 开启/关闭调试模式 |
server.port | 8080 | 服务器端口 |
server.servlet.context-path | /xxx | 应用上下文(用于区分Tomcat下部署的多个小webapp) |
spring.http.encoding.charset | utf-8 | 默认字符集编码 |
spring.thymeleaf.cache | true | 开启/关闭Thymeleaf模板页面缓存 |
spring.mvc.date-format | 日期输入格式 | |
spring.jackson.date-format | json输出转换的日期格式 | |
spring.jackson.time-zone | 设置GMT时区 |
日志常用配置项:
日志常用配置项 | 默认值 | 说明 |
---|---|---|
logging.file=xxxx | 日志输出的文件路径 | |
logging.level.ROOT | info | 设置日志的全局输出级别 |
logging.level.xxx | info | 设置指定package包的输出级别 |
logging.config | logback-spring.xml | 日志配置文件 |
日志级别:debug——>info——>warn——>error,debug输出的日志最多,error最少。
如果设置 debug=true 时,日志级别会自动调为 debug
Spring Boot 的简单实现
- 创建一个Maven项目
配置 pom.xml 管理依赖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<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.0</modelVersion>
<!-- 父项依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>testspringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testspringboot</name>
<description>Demo project for Spring Boot</description>
<!-- 指定jdk版本 -->
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- @SpringBootApplication 启动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- SpringMVC依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 单元测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- Maven 打包依赖 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建一个控制类,接受浏览器请求:C_Hello.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.example.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
public class C_Hello {
"/hello") //绑定该方法的URL地址。 (
//返回响应数据至浏览器。
public String hello(){
return "Hello Spring Boot!";
}
}
创建一个SpringBoot项目启动类:ApplicationRun.java
1
2
3
4
5
6
7
8
9
10
11
12package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class ApplicationRun {
public static void main(String[] args) {
SpringApplication.run(ApplicationRun.class,args);
}
}
运行ApplicationRun类,浏览器访问:http://localhost:8080/hello 显示:
1
Hello Spring Boot!
在 resource 目录下,创建 application.properties 文件,可对 Spring Boot 项目进行自定义配置。
Maven 用户可以继承
spring-boot-starter-parent
项以获得合理的默认值。父项目规定以下功能:- Java 1.8 是默认的编译器级别。
- UTF-8 源编码。
- 依赖性管理部分,继承自 spring-boot-dependencies pom,管理常见依赖项的版本。当在您自己的 pom 中使用时,这种依赖关系管理使您可以为这些依赖项省略
标记。 - 具有执行ID 的
repackage
goal的repackage
执行。 - 合理的 resource filtering.。
- 合理的插件配置 (exec plugin, Git commit ID, and shade)。
- 对 application.properties 和 application.yml 进行合理的资源过滤,包括特定于配置文件的文件(例如,application-dev.properties 和 application-dev.yml)
请注意,由于application.properties 和 application.yml文件接受Spring样式的占位符($ {…}),因此 Maven过滤已更改为使用@ .. @占位符。 (您可以通过设置一个名为 resource.delimiter 的 Maven 属性来覆盖它。)
注:若是使用 Idea 作为开发工具,可使用Spring Initializr 一键生成SpringBoot 项目
注册 Filter
Spring Boot中省去了 Web.xml 配置文件,所以注册 Filter 就需要在入口类中构建一个 FilterRegistrationBean 类型的方法来注册 Filter。
引入javax.servlet JAP包:
1 | <dependency> |
1.新建一个Filter类,编写过滤规则:拦截请求,获取客户端信息
1 | package com.example.wxshopapp.filter; |
2.在入口类注册Filter:
1 | package com.example.wxshopapp; |
3.编写一个测试类,访问并观察日志信息:日志是否包含“客户端信息:xxxxxx”
1 | package com.example.wxshopapp; |
Spring Boot支持的三种内嵌WEB容器
- Tomcat:默认,最流行的Web容器。
- Jetty:性能优秀的内嵌Web容器,适用于长连接。
- Undertow:非阻塞Web容器,性能优异,适用于高并发。
Spring Data与JPA
Spring Data项目是为了简化基于 Spring框架应用的数据访问架构。包括关系型数据库、非关系型数据库、Map-Reduce框架、云数据服务等等。
JPA是 Java Persistence API 的简称,中文名 Java 持久层 API,是 JDK 5.0 注解或 XML 描述对象 - 关系表的映射关系,并将运行期的实体对象持久化到数据库中。
连接池与Druid
连接池是创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用。
Spring Boot 对连接池支持:
- 目前Spring Boot 中默认支持的连接池有dbcp、dbcp2、tomcat2、hikari 三种连接池。
- 数据库连接可以使用 DataSource 池进行自动配置。
- 由于 Tomcat 数据源连接池的性能和并发,在 Tomcat 可用时,我们总是优先使用它。
- 如果 HikariCP 可用,我们将使用它。
- 如果 Commons DBCP可用,我们将使用它,但在生产环境不推荐使用它。
- 最后,如果 Commons DBCP2可用,我们将使用它。
而 Druid,是 JAVA 语言中最好的数据库连接池。它能够提供强大的监控和扩展功能。由[阿里巴巴]: https://github.com/alibaba/druid 开发。
2020年5月20日 完结
参考:51cto学院——《Spring Boot 小白轻松学SpringBoot 2》
以上内容,仅供参考,转载请注明出处。