Technical notes

Make a little progress every day


  • 首页

  • 归档

  • 分类

  • 标签

  • 关于

IntelliJ IDEA 的安装、配置与使用

发表于 2019-12-15 分类于 IDEA

IntelliJ IDEA 的安装、配置与使用

一、IntelliJ IDEA 介绍 -- Eclipse IBM

JetBrains 公司介绍

IDEA(https://[www.jetbrains.com/idea/) 是JetBrains 公司的产品,公司旗下还有其它产品,比如:

  • WebStorm:用于开发 JavaScript、HTML5、CSS3 等前端技术;

  • PyCharm:用于开发 python

  • PhpStorm:用于开发 PHP

  • RubyMine:用于开发 Ruby/Rails

  • AppCode:用于开发 Objective - C/Swift

  • CLion:用于开发 C/C++

  • DataGrip:用于开发数据库和 SQL

  • Rider:用于开发.NET

  • GoLand:用于开发 Go

  • Android Studio:用于开发 android(google 基于IDEA 社区版进行迭代)

IntelliJ IDEA 介绍

IDEA,全称 IntelliJ IDEA,是 Java 语言的集成开发环境,IDEA 在业界被公认为是最好的 java 开发工具之一,尤其在智能代码助手、代码自动提示、重构、J2EE 支持、Ant、JUnit、CVS 整合、代码审查、创新的GUI 设计等方面的功能可以说是超常的。

IntelliJ IDEA 在 2015 年的官网上这样介绍自己:

Excel at enterprise, mobile and web development with Java, Scala and Groovy, with all the latest modern technologies and frameworks available out of the box.

简明翻译:IntelliJ IDEA 主要用于支持 Java、Scala、Groovy 等语言的开发工具,同时具备支持目前主流的技术和框架,擅长于企业应用、移动应用和 Web 应用的开发。

IDEA 的主要功能介绍

语言支持上:

{width=”3.4368646106736658in” height=”2.75in”}

其他支持:

{width=”4.322913385826772in” height=”4.333333333333333in”}

IDEA 的主要优势:(相较于 Eclipse 而言)

① 强大的整合能力。比如:Git、Maven、Spring 等

② 提示功能的快速、便捷

③ 提示功能的范围广

{width=”6.73372375328084in” height=”1.2104166666666667in”}

④ 好用的快捷键和代码模板 private static final psf

⑤ 精准搜索

IDEA 的下载地址:(官网)

https://www.jetbrains.com/idea/download/#section=windows

IDEA 分为两个版本:旗舰版(Ultimate)和社区版(Community)。

旗舰版收费(限 30 天免费试用),社区版免费,这和 Eclipse 有很大区别。

{width=”6.575725065616798in” height=”5.4in”}

这里提供了不同操作系统下的两个不同版本的安装文件。两个不同版本的详细对比,可以参照官网:

[https://www.jetbrains.com/idea/features/editions_comparison_matrix.html]{.underline}

官网提供的详细使用文档

[https://www.jetbrains.com/help/idea/meet-intellij-idea.html]{.underline}

二、windows 下安装过程

1. 安装前的准备

1.1 硬件要求(Hardware requirements)

个人建议配置:内存 8G 或以上,CPU 最好 i5 以上,最好安装块固态硬盘(SSD),将 IDEA

安装在固态硬盘上,这样流畅度会加快很多。

1.2 软件要求(Software requirements)

操作系统:Microsoft Windows 10/8/7/Vista/2003/XP (32 or 64 bit)

软件环境:

注意:这里如果没有安装 JDK 的话,请参考提供的文档《尚硅谷_宋红康_JDK8 的下载_安装_ 配置.pdf》进行安装配置。

2. 具体安装过程

双击:

{width=”2.653441601049869in” height=”0.65625in”}

{width=”5.343952318460192in” height=”4.135416666666667in”}

{width=”5.3421620734908135in” height=”4.15625in”}

{width=”5.3421620734908135in” height=”4.15625in”}

  • 确认 32 位版还是 64 位版

  • 确认是否与.java、.groovy、.kt 格式文件进行关联,这里也可以选择不关联。

{width=”5.260944881889764in” height=”4.094061679790026in”}

{width=”5.343943569553805in” height=”4.177083333333333in”}

{width=”5.289893919510061in” height=”4.145833333333333in”}

3. 安装总结

从安装上来看,IntelliJ IDEA 对硬件的要求似乎不是很高。可是实际在开发中其实并不是这样的,因为 IntelliJ IDEA 执行时会有大量的缓存、索引文件,所以如果你正在使用 Eclipse / MyEclipse,想通过 IntelliJ IDEA 来解决计算机的卡、

慢等问题,这基本上是不可能的,本质上你应该对自己的硬件设备进行升级。

4. 查看安装目录结构

{width=”5.986007217847769in” height=”3.342187226596675in”}

其中:bin 目录下:

{width=”5.460259186351706in” height=”1.96875in”}

这里以我的电脑系统(64 位windows7,16G 内存)为例,说明一下如何调整 VM 配置文件:

  1. 大家根据电脑系统的位数,选择 32 位的 VM 配置文件或者 64 位的VM 配置文件

  2. 32 位操作系统内存不会超过 4G,所以没有多大空间可以调整,建议不用调整了

  3. 64 位操作系统中 8G 内存以下的机子或是静态页面开发者是无需修改的。

  4. 64 位操作系统且内存大于 8G 的,如果你是开发大型项目、Java 项目或是 Android 项目, 建议进行修改,常修改的就是下面 3 个参数:

5. 查看设置目录结构

{width=”5.9885444006999125in” height=”2.1078116797900264in”}

这是 IDEA 的各种配置的保存目录。这个设置目录有一个特性,就是你删除掉整个目录之后,重新启动 IntelliJ IDEA 会再自动帮你生成一个全新的默认配置, 所以很多时候如果你把 IntelliJ IDEA 配置改坏了,没关系,删掉该目录,一切都会还原到默认。

5.1 config 目录

{width=”6.322916666666667in” height=”2.147222222222222in”}config 目录是 IntelliJ IDEA 个性化化配置目录,或者说是整个 IDE 设置目录。此目录可看成是最重要的目录,没有之一,如果你还记得安装篇的介绍的时候, 安装新版本的 IntelliJ IDEA 会自动扫描硬盘上的旧配置目录,指的就是该目录。这个目录主要记录了:IDE 主要配置功能、自定义的代码模板、自定义的文件模板、自定义的快捷键、Project 的 tasks 记录等等个性化的设置。 比如:

5.2 system 目录

system 目录是 IntelliJ IDEA 系统文件目录,是 IntelliJ IDEA 与开发项目一个桥梁目录,里面主要有:缓存、索引、容器文件输出等等,虽然不是最重要目录, 但也是最不可或缺的目录之一。比如:

{width=”5.751617454068241in” height=”2.46875in”}

三、启动应用后简单配置

1. 是否导入已有设置

首次启动,会弹出如下的对话框。选择不导入已有的设置。

{width=”5.394561461067367in” height=”1.90625in”}

2. 激活

然后根据提供的激活文档《IDEA2017-2018_激活方法》或百度:idea 破解码,填入:lisence server 的具体值即可。(需要联网)或者 选择Activation code,根据

文档提供的激活码,同样可以激活。(不需要联网)

{width=”4.256418416447944in” height=”3.7109372265966756in”}

补充:

对于 IDEA 2017.3 月版本,需要按照如下的方式激活: 方式一:

启动:idea_active_proxy 激活代理.exe

接着,启动 IDEA:输入 server: http://localhost:8888 ,然后可以关闭

idea_active_proxy 工具。方式二:淘宝,你懂的

3. 设置主题

{width=”6.302637795275591in” height=”3.8475in”}

这里根据个人喜好,进行选择,也可以选择跳过(skip all and set defaults)。后面在

settings 里也可以再设置主题等。这里选择:Next:Default plugins

4. 设置插件

{width=”5.889348206474191in” height=”3.89375in”}

{width=”5.991135170603674in” height=”4.019687226596675in”}

设置 IDEA 中的各种插件,可以选择自定义设置、删除,或者安装本身不存在的插件(比如:支持 Scala 的插件)。这里不设置,后面也可以通过界面菜单栏的

settings 进行设置。

IDEA 插件官方下载地址:[https://plugins.jetbrains.com/idea]{.underline}

5. 启动页面

{width=”5.759499125109361in” height=”3.60125in”}

四、创建 Java 工程,运行 HelloWorld

1. 创建 Java 工程

{width=”6.7583169291338585in” height=”4.947916666666667in”}

  • Create New Project:创建一个新的工程

  • Import Project:导入一个现有的工程

  • Open:打开一个已有工程。比如:可以打开 Eclipse 项目。

  • Check out from Version Control:可以通过服务器上的项目地址 check out Github

上面项目或其他Git 托管服务器上的项目

这里选择 Create New Project,需要明确一下概念:

IntelliJ IDEA 没有类似 Eclipse 的工作空间的概念(Workspaces),最大单元就是

Project。这里可以把 Project 理解为Eclipse 中的 Workspace。

选择指定目录下的JDK 作为Project SDK。

如果要创建Web 工程,则需要勾选上面的 Web Application。如果不需要创建 Web

工程的话,则不需要勾选。这里先不勾选,只是创建简单的 Java 工程。

其中,选择New:

选择 jdk 的安装路径所在位置:

{width=”4.563805774278215in” height=”5.177083333333333in”}

点击 OK 以后,选择Next:

{width=”5.968839676290464in” height=”3.572187226596675in”}

这里不用勾选。选择 Next,进入下一个页面:

{width=”6.019655511811024in” height=”4.461248906386702in”}

给创建的工程起一个名字,点击 finish。

{width=”3.856498250218723in” height=”1.6354166666666667in”}

点击 OK 即可。

2. 设置显示常见的视图

{width=”2.6151771653543308in” height=”2.750624453193351in”}

调出工具条和按钮组

3. 工程界面展示

{width=”7.5541458880139984in” height=”4.008124453193351in”}

  • 工程下的src 类似于 Eclipse 下的 src 目录,用于存放代码。

  • 工程下的.idea 和 project01.iml 文件都是 IDEA 工程特有的。类似于 Eclipse 工程下的.settings、.classpath、.project 等。

4. 创建 package 和 class

接着在 src 目录下创建一个 package:

{width=”6.728131014873141in” height=”2.0309372265966754in”}{width=”4.458333333333333in” height=”1.4270833333333333in”}

在包下 new-class:

{width=”6.740869422572178in” height=”1.44in”}{width=”3.2127034120734907in” height=”2.1141666666666667in”}

不管是创建 class,还是 interface,还是 annotation,都是选择 new – java class,

然后在下拉框中选择创建的结构的类型。

{width=”6.696037839020122in” height=”3.36875in”}接着在类 HelloWorld 里声明主方法,输出 helloworld,完成测试。

说明:在 IDEA 里要说的是,写完代码,不用点击保存。IDEA 会自动保存代码。

5. 创建模块(Module)

  1. 在Eclipse 中我们有Workspace(工作空间)和 Project(工程)的概念,在 IDEA

中只有 Project(工程)和 Module(模块)的概念。这里的对应关系为:

1
2
3
4
5
6
IDEA 官网说明:
An Eclipse workspace is similar to a project in IntelliJ IDEA
An Eclipse project maps to a module in IntelliJ IDEA
翻译:
Eclipse 中 workspace 相当于 IDEA 中的 Project
Eclipse 中 Project 相当于 IDEA 中的 Module

这个地方刚开始用的时候会很容易理不清它们之间的关系。

  1. 从 Eclipse 转过来的人总是下意识地要在同一个窗口管理 n 个项目,这在IntelliJ IDEA 是无法做到的。IntelliJ IDEA 提供的解决方案是打开多个项目实例, 即打开多个项目窗口。即:一个 Project 打开一个 Window 窗口。

  2. 在 IntelliJ IDEA 中 Project 是最顶级的级别,次级别是 Module。一个 Project

可以有多个 Module。目前主流的大型项目都是分布式部署的,结构都是类似这种多 Module 结构。

{width=”6.396527777777778in” height=”2.1666666666666665in”}

{width=”3.1465277777777776in” height=”3.7618055555555556in”}这类项目一般是这样划分的,比如:core Module、web Module、plugin Module、solr Module 等等,模块之间彼此可以相互依赖。通过这些 Module 的命名也可以看出,他们之间都是处于同一个项目业务下的模块,彼此之间是有不可分割的业务关系的。举例:

  1. 相比较于多 Module 项目,小项目就无需搞得这么复杂。只有一个 Module 的结构 IntelliJ IDEA 也是支持的,并且 IntelliJ IDEA 创建项目的时候,默认就是单

Module 的结构的。

{width=”5.693474409448819in” height=”1.0541655730533683in”}{width=”6.279263998250219in” height=”4.363333333333333in”}下面,我们演示如何创建 Module:

接着选择Next:

{width=”6.718850612423447in” height=”4.3888538932633425in”}

之后,我们可以在 Module 的src 里写代码,此时 Project 工程下的src 就没什么用了。可以删掉。

6. 如何删除模块

{width=”4.367793088363954in” height=”5.075in”}{width=”3.4208858267716535in” height=”1.6340616797900263in”}{width=”3.9154090113735784in” height=”1.40625in”}

{width=”4.383336614173229in” height=”4.395833333333333in”}

此时的删除,会从硬盘上将此module 删除掉。

7. 查看项目配置

{width=”3.301388888888889in” height=”2.7916666666666665in”}

进入项目结构:

{width=”6.812560148731409in” height=”3.6666666666666665in”}

五、常用配置

IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让那些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它。

{width=”2.8965266841644794in” height=”3.125in”}进入设置界面:

{width=”5.983117891513561in” height=”3.1626038932633422in”}目录结构如下:

1. Appearance & Behavior

1.1 设置主题

{width=”6.011954286964129in” height=”2.217603893263342in”}

这里默认提供了三套主题:IntelliJ,Darcula,Windows。这里可以根据自己的喜好进行选择。

1.2 设置窗体及菜单的字体及字体大小 (可忽略)

{width=”5.981924759405074in” height=”1.76375in”}

  1. 补充:设置编辑区主题 (可忽略)

IDEA 默认提供了两个编辑区主题,可以通过如下的方式进行选择。

{width=”6.114583333333333in” height=”2.4166666666666665in”}

  • 如果想要更多的主题效果的话,可以到如下的网站下载:

http://www.riaway.com/

  • 下载以后,导入主题:(方式一)

file –> import setttings –> 选中下载的主题 jar 文件 –> 一路确认 –> 重启。重启以后,新主题会自动启用。如果没有启用,可以如下方式选择:

{width=”6.396527777777778in” height=”2.5416666666666665in”}

  • {width=”6.732657480314961in” height=”1.8389577865266842in”}下载以后,导入主题:(方式二)

1.3 补充:通过插件(plugins)更换主题

喜欢黑色主题的话,还可以下载插件:Material Theme UI

{width=”6.001079396325459in” height=”4.058124453193351in”}

{width=”5.987691382327209in” height=”2.033332239720035in”}点击按钮以后,在联网环境下搜索如下的插件-安装-重启 IDEA 即可:

如果对安装的主题插件不满意,还可以找到此插件,进行卸载 – 重启 IDEA 即可。

2. Editor - General

2.1 设置鼠标滚轮修改字体大小(可忽略)

{width=”5.992442038495188in” height=”2.0924989063867017in”}

我们可以勾选此设置后,增加 Ctrl + 鼠标滚轮 快捷键来控制代码字体大小显示。

2.2 设置鼠标悬浮提示

{width=”5.937643263342082in” height=”3.43125in”}

2.3 设置自动导包功能

{width=”5.98492125984252in” height=”3.6968744531933506in”}

  • Add unambiguous imports on the fly:自动导入不明确的结构

  • Optimize imports on the fly:自动帮我们优化导入的包

2.4 设置显示行号和方法间的分隔符

{width=”5.93623687664042in” height=”3.41875in”}

  • 如上图红圈所示,可以勾选 Show line numbers:显示行数。我建议一般这个要勾选上。

  • 如上图红圈所示,可以勾选 Show method separators: 显示方法分隔线。这种线有助于我们区分开方法,所以建议勾选上。

2.5 忽略大小写提示

idea2019.3 设置方法:Editor->General->Code Completion 第一行取消选中”Match case”(默认严格匹配)

2019.3之前版本设置方法如下:

{width=”6.037877296587927in” height=”3.493333333333333in”}

  • IntelliJ IDEA 的代码提示和补充功能有一个特性:区分大小写。如上图标注所示,默认就是 First letter 区分大小写的。

  • 区分大小写的情况是这样的:比如我们在 Java 代码文件中输入 stringBuffer, IntelliJ IDEA 默认是不会帮我们提示或是代码补充的,但是如果我们输入

StringBuffer 就可以进行代码提示和补充。

  • 如果想不区分大小写的话,改为 None 选项即可。

2.6 设置取消单行显示 tabs 的操作

{width=”5.963204286964129in” height=”2.727811679790026in”}

如上图标注所示,在打开很多文件的时候,IntelliJ IDEA 默认是把所有打开的文件名 Tab 单行显示的。但是我个人现在的习惯是使用多行,多行效率比单行高, 因为单行会隐藏超过界面部分 Tab,这样找文件不方便。

3. Editor – Font

3.1 设置默认的字体、字体大小、字体行间距

{width=”6.7721708223972in” height=”2.1009372265966753in”}

4. Editor – Color Scheme

4.1 修改当前主题的字体、字体大小、字体行间距(可忽略)

如果当前主题不希望使用默认字体、字体大小、字体行间距,还可以单独设置:

{width=”6.764671916010498in” height=”1.9583333333333333in”}

4.2 修改当前主题的控制台输出的字体及字体大小(可忽略)

{width=”6.624998906386701in” height=”2.5729166666666665in”}

4.3 修改代码中注释的字体颜色

{width=”6.7044870953630795in” height=”3.5781244531933507in”}

  • Doc Comment – Text:修改文档注释的字体颜色

  • Block comment:修改多行注释的字体颜色

  • Line comment:修改当行注释的字体颜色

5. Editor – Code Style

5.1 设置超过指定 import 个数,改为* (可忽略)

{width=”6.733333333333333in” height=”2.341665573053368in”}

6. Editor – File and Code Templates

6.1 修改类头的文档注释信息

{width=”6.035658355205599in” height=”2.2333333333333334in”}

常用的预设的变量,这里直接贴出官网给的:

7. Editor – File Encodings

7.1 设置项目文件编码

{width=”6.7993121172353455in” height=”3.9425in”}

说明:Transparent native-to-ascii conversion 主要用于转换 ascii,一般都要勾选, 不然 Properties 文件中的注释显示的都不会是中文。

7.2 设置当前源文件的编码(可忽略)

{width=”5.994523184601925in” height=”3.153332239720035in”}

{width=”5.902446412948382in” height=”1.6979166666666667in”}

对单独文件的编码修改还可以点击右下角的编码设置区。如果代码内容中包含中文,则会弹出如上的操作选择。其中:

①Reload 表示使用新编码重新加载,新编码不会保存到文件中,重新打开此文件,旧编码是什么依旧还是什么。

②Convert 表示使用新编码进行转换,新编码会保存到文件中,重新打开此文件, 新编码是什么则是什么。

③含有中文的代码文件,Convert 之后可能会使中文变成乱码,所以在转换成请做好备份,不然可能出现转换过程变成乱码,无法还原。

8. Build,Execution,Deployment

8.1 设置自动编译

{width=”5.972542650918635in” height=”3.5011450131233595in”}

  • 构建就是以我们编写的 java 代码、框架配置文件、国际化等其他资源文件、

JSP 页面和图片等资源作为”原材料”,去”生产”出一个可以运行的项目的过程。

  • Intellij Idea 默认状态为不自动编译状态,Eclipse 默认为自动编译:

{width=”2.5833333333333335in” height=”2.65625in”}很多朋友都是从 Eclipse 转到 Intellij 的,这常常导致我们在需要操作 class 文件时忘记对修改后的 java 类文件进行重新编译,从而对旧文件进行了操作。

9. 设置为省电模式 (可忽略)

{width=”3.0003772965879265in” height=”5.520833333333333in”}

如上图所示,IntelliJ IDEA 有一种叫做 省电模式 的状态,开启这种模式之后IntelliJ IDEA 会关掉代码检查和代码提示等功能。所以一般也可认为这是一种 阅读模式,如果你在开发过程中遇到突然代码文件不能进行检查和提示,可以来看看这里是否有开启该功能。

10. 设置代码水平或垂直显示

在编辑区上方的文件名上点击鼠标右键

{width=”5.196393263342082in” height=”2.929165573053368in”}

六、设置快捷键(Keymap)

6.1 设置快捷为 Eclipse 的快捷键

{width=”6.665906605424322in” height=”3.99625in”}

6.2 通过快捷键功能修改快捷键设置

{width=”6.73540135608049in” height=”4.083333333333333in”}

6.3 通过指定快捷键,查看或修改其功能

{width=”5.95587489063867in” height=”3.5714577865266843in”}

6.4 导入已有的设置

{width=”2.915938320209974in” height=”2.78125in”}{width=”4.489583333333333in” height=”5.291666666666667in”}

点击 0K 之后,重启IDEA 即可。

6.5 常用快捷键

序号 功能 快捷键
1 执行(run) alt+r
2
3
4
5
6
7
8
9
10

+—————————-+——————————————————+——————————+
| > 尚硅谷·宋红康 设置版 | | |
+============================+======================================================+==============================+
| 1 | 执行(run) | alt+r |
+—————————-+——————————————————+——————————+
| 2 | 提示补全 (Class Name Completion) | alt+/ |
+—————————-+——————————————————+——————————+
| 3 | 单行注释 | ctrl + / |
+—————————-+——————————————————+——————————+
| 4 | 多行注释 | ctrl + shift + / |
+—————————-+——————————————————+——————————+
| 5 | 向下复制一行 (Duplicate Lines) | ctrl+alt+down |
+—————————-+——————————————————+——————————+
| 6 | 删除一行或选中行 (delete line) | ctrl+d |
+—————————-+——————————————————+——————————+
| 7 | 向下移动行(move statement down) | alt+down |
+—————————-+——————————————————+——————————+
| 8 | 向上移动行(move statement up) | alt+up |
+—————————-+——————————————————+——————————+
| 9 | 向下开始新的一行(start new line) | shift+enter |
+—————————-+——————————————————+——————————+
| 10 | 向上开始新的一行 (Start New Line before current) | ctrl+shift+enter |
+—————————-+——————————————————+——————————+
| 11 | 如何查看源码 (class) | ctrl + 选中指定的结构 或 |
| | | |
| | | ctrl + shift + t |
+—————————-+——————————————————+——————————+
| 12 | 万能解错/生成返回值变量 | alt + enter |
+—————————-+——————————————————+——————————+
| 13 | 退回到前一个编辑的页面 (back) | alt + left |
+—————————-+——————————————————+——————————+
| 14 | 进入到下一个编辑的页面(针对于上条) (forward) | alt + right |
+—————————-+——————————————————+——————————+
| 15 | 查看继承关系(type hierarchy) | F4 |
+—————————-+——————————————————+——————————+
| 16 | 格式化代码(reformat code) | ctrl+shift+F |
+—————————-+——————————————————+——————————+
| 17 | 提示方法参数类型(Parameter Info) | ctrl+alt+/ |
+—————————-+——————————————————+——————————+
| 18 | 复制代码 | ctrl + c |
+—————————-+——————————————————+——————————+
| 19 | 撤销 | ctrl + z |
+—————————-+——————————————————+——————————+
| 20 | 反撤销 | ctrl + y |
+—————————-+——————————————————+——————————+
| 21 | 剪切 | ctrl + x |
+—————————-+——————————————————+——————————+
| 22 | 粘贴 | ctrl + v |
+—————————-+——————————————————+——————————+
| 23 | 保存 | ctrl + s |
+—————————-+——————————————————+——————————+
| 24 | 全选 | ctrl + a |
+—————————-+——————————————————+——————————+
| 25 | 选中数行,整体往后移动 | tab |
+—————————-+——————————————————+——————————+
| 26 | 选中数行,整体往前移动 | shift + tab |
+—————————-+——————————————————+——————————+
| 27 | 查看类的结构:类似于 eclipse 的 outline | ctrl+o |
+—————————-+——————————————————+——————————+
| 28 | 重构:修改变量名与方法名(rename) | alt+shift+r |
+—————————-+——————————————————+——————————+
| 29 | 大写转小写/小写转大写(toggle case) | ctrl+shift+y |
+—————————-+——————————————————+——————————+

30 生成构造器/get/set/toString alt +shift + s


31 查看文档说明(quick documentation) F2
32 收起所有的方法(collapse all) alt + shift + c
33 打开所有方法(expand all) alt+shift+x
34 打开代码所在硬盘文件夹(show in explorer) ctrl+shift+x
35 生成 try-catch 等(surround with) alt+shift+z
36 局部变量抽取为成员变量(introduce field) alt+shift+f
37 查找/替换(当前) ctrl+f
38 查找(全局) ctrl+h
39 查找文件 double Shift
40 查看类的继承结构图(Show UML Diagram) ctrl + shift + u
41 查看方法的多层重写结构(method hierarchy) ctrl+alt+h
42 添加到收藏(add to favorites) ctrl+alt+f
43 抽取方法(Extract Method) alt+shift+m
44 打开最近修改的文件(Recently Files) ctrl+E
45 关闭当前打开的代码栏(close) ctrl + w
46 关闭打开的所有代码栏(close all) ctrl + shift + w
47 快速搜索类中的错误(next highlighted error) ctrl + shift + q
48 选择要粘贴的内容(Show in Explorer) ctrl+shift+v
49 查找方法在哪里被调用(Call Hierarchy) ctrl+shift+h

七、关于模板(Templates)

(Editor – Live Templates 和Editor – General – Postfix Completion)

7.1 Live Templates(实时代码模板)功能介绍

它的原理就是配置一些常用代码字母缩写,在输入简写时可以出现你预定义的固

定模式的代码,使得开发效率大大提高,同时也可以增加个性化。最简单的例子就是在 Java 中输入 sout 会出现System.out.println();

官方介绍 Live Templates:

https://www.jetbrains.com/help/idea/using-live-templates.html

7.2 已有的常用模板

{width=”6.3129790026246715in” height=”5.335in”}7.2.1 Postfix Completion 默认如下:

7.2.2 Live Templates 默认如下:

{width=”6.774219160104987in” height=”3.65625in”}

二者的区别:Live Templates 可以自定义,而 Postfix Completion 不可以。同时, 有些操作二者都提供了模板,Postfix Templates 较 Live Templates 能快 0.01 秒

7.2.3 举例

1. psvm : 可生成 main 方法

2. sout : System.out.println() 快捷输出

类似的:

soutp=System.out.println(\”方法形参名 = \” + 形参名);

soutv=System.out.println(\”变量名 = \” + 变量);

soutm=System.out.println(\”当前类名.当前方法\”); “abc”.sout => System.out.println(\”abc\”);

3. fori : 可生成 for 循环

类似的:

iter:可生成增强for 循环

itar:可生成普通 for 循环

4. list.for : 可生成集合 list 的 for 循环

List\<String> list = new ArrayList\<String>();

输入: list.for 即可输出

for(String s:list){

}

又如:list.fori 或 list.forr

5. ifn:可生成 if(xxx = null)

类似的:

inn:可生成if(xxx != null) 或 xxx.nn 或 xxx.null

6. prsf:可生成 private static final

类似的:

psf:可生成 public static final

psfi:可生成 public static final int psfs:可生成 public static final String

7.3 修改现有模板:Live Templates

如果对于现有的模板,感觉不习惯、不适应的,可以修改:

修改 1:

通过调用 psvm 调用 main 方法不习惯,可以改为跟 Eclipse 一样,使用 main 调取。

修改 2:

{width=”5.696527777777778in” height=”3.75in”}

类似的还可以修改psfs。

7.4 自定义模板

IDEA 提供了很多现成的 Templates 。但你也可以根据自己的需要创建新的

{width=”6.401606517935258in” height=”2.4791666666666665in”}Template。

先定义一个模板的组:

{width=”3.9766557305336834in” height=”1.5208333333333333in”}{width=”6.0297823709536305in” height=”2.3197911198600174in”}

选中自定义的模板组,点击”+”来定义模板。

  1. Abbreviation:模板的缩略名称

  2. Description:模板的描述

  3. Template text:模板的代码片段

  4. {width=”2.4583333333333335in” height=”1.6770833333333333in”}应用范围。比如点击 Define。选择如下:

可以如上的方式定义个测试方法,然后在 java 类文件中测试即可。类似的可以再配置如下的几个 Template:

{width=”6.690655074365704in” height=”2.0521872265966756in”}1.

2.

{width=”6.7637051618547686in” height=”2.0104166666666665in”}

八、创建 Java Web Project或Module

1. 创建的静态 Java Web

{width=”5.417141294838145in” height=”3.685in”}

{width=”4.833464566929134in” height=”2.6365616797900264in”}

2. 创建动态的 Java Web

2.1 创建动态 Web 的 module

工程栏空白处new – module:

{width=”5.964782370953631in” height=”3.01in”}

{width=”5.983720472440945in” height=”4.3275in”}

这里一定要勾选Web Application,才能创建一个Web 工程。

{width=”6.014680664916885in” height=”2.117707786526684in”}

提供 Web 工程名。这里注意修改一下Content root 和 Module file location。创建以后的工程结构如下:

{width=”3.4394061679790027in” height=”1.96875in”}

打开 index.jsp。修改为如下内容。这里你会发现 IDEA 的代码提示功能要强于

Eclipse。

2.2 配置 Tomcat

在 IDEA 中配置 Tomcat 之前,需要保证已经安装并配置了 Tomcat 的环境变量。如果没有安装并配置,可以参考《尚硅谷_宋红康_Tomcat 快速部署.pdf》,配置完成以后,在命令行输入:catalina run 。能够启动 tomcat,则证明安装配置成功。

下面看如何在 IDEA 中配置:

{width=”4.5in” height=”2.2083333333333335in”}

点击 Edit Configurations:

{width=”4.8441601049868765in” height=”4.0396872265966755in”}

这里选择 TomEE Server 或者 Tomcat Server 都可以。接着选择 Local。

{width=”6.772146762904637in” height=”4.338541119860017in”}

这里配置 Tomcat 的名称以及配置应用服务器的位置。根据自己 Tomcat 的安装位置决定。

{width=”6.136371391076115in” height=”4.171874453193351in”}

其它位置使用默认值(设置要启动的浏览器以及端口号),如上。

{width=”6.690101706036746in” height=”1.9170822397200349in”}{width=”6.7398764216972875in” height=”2.1458333333333335in”}接着部署:

点击 OK 即可。此时:

{width=”6.704075896762904in” height=”2.1875in”}

执行刚才创建的 index.jsp 即可:

{width=”4.075717410323709in” height=”1.0104166666666667in”}

效果如下:

{width=”3.9895833333333335in” height=”1.375in”}

注意事项:

{width=”6.011174540682415in” height=”1.70625in”}显示运行以后的 Tomcat 的信息:

可以点击红框,刚点击完毕并不能马上关闭服务器,只是断开了与服务器的连接, 稍后当停止按钮显示为灰色,才表示关闭。

九、关联数据库

1. 关联方式

{width=”4.370374015748031in” height=”4.265624453193351in”}

{width=”6.743641732283464in” height=”2.31in”}

表面上很多人认为配置 Database 就是为了有一个 GUI 管理数据库功能,但是这并不是 IntelliJ IDEA 的 Database 最重要特性。数据库的 GUI 工具有很多, IntelliJ IDEA 的 Database 也没有太明显的优势。IntelliJ IDEA 的 Database 最大特性就是对于 Java Web 项目来讲,常使用的 ORM 框架,如 Hibernate、Mybatis 有很好的支持,比如配置好了 Database 之后,IntelliJ IDEA 会自动识别 domain 对象与数据表的关系,也可以通过 Database 的数据表直接生成 domain 对象等等。

2. 常用操作

{width=”2.313905293088364in” height=”2.2604166666666665in”}

十、版本控制(Version Control)

不管是个人开发还是团队开发,版本控制都会被使用。而 IDEA 也很好的集成了版本控制的相关结构。

{width=”6.032199256342957in” height=”4.690624453193351in”}

  • {width=”6.728472222222222in” height=”2.0763877952755907in”}很多人认为 IntelliJ IDEA 自带了 SVN 或是 Git 等版本控制工具,认为只要安装了 IntelliJ IDEA 就可以完全使用版本控制应有的功能。这完全是一种错误的解读,IntelliJ IDEA 是自带对这些版本控制工具的插件支持,但是该装什么版本控制客户端还是要照样装的。

  • IntelliJ IDEA 对版本控制的支持是以插件化的方式来实现的。旗舰版默认支持目前主流的版本控制软件:CVS、Subversion(SVN)、Git、Mercurial、

Perforce、TFS。又因为目前太多人使用 Github 进行协同或是项目版本管理, 所以 IntelliJ IDEA 同时自带了 Github 插件,方便 Checkout 和管理你的

Github 项目。

  • 在实际开发中,发现在 IDEA 中使用SVN 的经历不算愉快,经常会遇到很多问题,比如紧急情况下 IDEA 无法更新、提交等。所以这里,谈下在 IDEA 中使用 Git。

1. 提前安装好 Git 的客户端

Git 的msysGit 官网下载:https://git-scm.com/

Git 客户端 TortoiseGit 官网下载:http://download.tortoisegit.org/tgit/

2. 关联 git.exe

{width=”6.688386920384952in” height=”2.0718744531933506in”}

3. 关联 GitHub 上的账户,并测试连接

{width=”6.711497156605424in” height=”1.7609372265966754in”}

4. 在 GitHub 上创建账户下的一个新的仓库作为测试

{width=”6.740038276465442in” height=”1.1735411198600174in”}{width=”6.687450787401575in” height=”3.7133333333333334in”}

5. 支持从当前登录的 Github 账号上直接 Checkout 项目

{width=”6.6995133420822395in” height=”2.185in”}

6. 在 IDEA 中 clone GitHub 上的仓库:

{width=”6.622688101487314in” height=”1.8703116797900263in”}

{width=”6.33556539807524in” height=”2.970832239720035in”}这里需要在 GitHub 的自己的账户下,复制项目仓库路径,填写到上图 Git Repository URL 中。如下:

7. 连接成功以后,会下载 github 上的项目

{width=”4.896713692038495in” height=”4.416561679790027in”}

{width=”4.939490376202975in” height=”4.433333333333334in”}

{width=”6.691889763779527in” height=”1.2271872265966755in”}

根据自己的需要,选择本窗口,还是开启一个新的窗口。

8. 除此之外,还可以通过如下的方式连接 GitHub

{width=”6.719156824146982in” height=”1.58in”}

9. 本地代码分享到 GitHub

{width=”6.095722878390201in” height=”4.151666666666666in”}

{width=”5.065201224846894in” height=”1.3020833333333333in”}

此时会在GitHub 上创建一个新的仓库,而非更新已经存在的仓库。

10. Git 的常用操作

{width=”6.614452099737533in” height=”2.6953116797900263in”}

  1. 没有使用 Git 时本地历史记录的查看

{width=”6.713824365704287in” height=”3.7640616797900264in”}{width=”6.785765529308836in” height=”2.84in”}

即使我们项目没有使用版本控制功能,IntelliJ IDEA 也给我们提供了本地文件历史记录。

十一、断点调试

1. Debug 的设置

{width=”6.7506616360454945in” height=”2.5565616797900264in”}

设置 Debug 连接方式,默认是 Socket。Shared memory 是 Windows 特有的一个属性,一般在 Windows 系统下建议使用此设置,内存占用相对较少。

2. 常用断点调试快捷键

对于常用的 Debug 的快捷键,需要大家熟练掌握。

3. 条件断点

3.1 说明

调试的时候,在循环里增加条件判断,可以极大的提高效率,心情也能愉悦。具体操作:

在断点处右击调出条件断点。可以在满足某个条件下,实施断点。

3.2 查看表达式的值(Ctrl + u)

选择行,ctrl + u。还可以在查看框中输入编写代码时的其他方法:

{width=”5.427713254593176in” height=”2.84375in”}

十二、配置 Maven

1. Maven 的介绍

{width=”2.050391513560805in” height=”0.5174989063867017in”}

Make -> Ant -> Maven -> Gradle

Maven 是Apache 提供的一款自动化构建工具,用于自动化构建和依赖管理。开发团队基本不用花多少时间就能自动完成工程的基础构建配置,因为 Maven

使用了一个标准的目录结构和一个默认的构建生命周期。在如下环节中,Maven 使得开发者工作变得更简单。

构建环节:

2. Maven 的配置

maven 的下载 – 解压 – 环境变量的配置这里就赘述了,需要的参考 1-课件中的《Maven 的配置》。下面直接整合 Maven。选择自己 Maven 的目录,和 settings 文件,然后配置自己的仓库 reposiroty。

{width=”6.68189523184602in” height=”2.7041666666666666in”}

  • Maven home directory:可以指定本地 Maven 的安装目录所在,因为我已经配置了

M2_HOME 系统参数,所以直接这样配置 IntelliJ IDEA 是可以找到的。但是假如你没有配置的话,这里可以选择你的 Maven 安装目录。此外,这里不建议使用 IDEA 默认的。

  • User settings file / Local repository:我们还可以指定 Maven 的 settings.xml 位置和本地仓库位置。

{width=”6.743719378827646in” height=”3.15625in”}

  • Import Maven projects automatically:表示 IntelliJ IDEA 会实时监控项目的 pom.xml 文件, 进行项目变动设置。

  • Automatically download:在 Maven 导入依赖包的时候是否自动下载源码和文档。默认是没有勾选的,也不建议勾选,原因是这样可以加快项目从外网导入依赖包的速度,如果我们需要源码和文档的时候我们到时候再针对某个依赖包进行联网下载即可。IntelliJ IDEA 支持直接从公网下载源码和文档的。

  • VM options for importer:可以设置导入的 VM 参数。一般这个都不需要主动改,除非项目真的导入太慢了我们再增大此参数。

3. 创建对应的 Module

{width=”6.71659230096238in” height=”1.0713538932633422in”}

{width=”6.7352712160979875in” height=”2.6535411198600176in”}

举例:此时 Spring Initalizr 是 springboot 工程的模板。

{width=”6.748010717410324in” height=”4.077082239720035in”}

Group:组织或公司域名,倒序

Artifact:项目模块名称

Version:默认 maven 生成版本:0.0.1-SNAPSHOT

{width=”6.771725721784777in” height=”2.7916666666666665in”}

这里可以暂时先不选,后面开发需要了再进行设置。

{width=”6.759722222222222in” height=”1.3755205599300087in”}

点击 finish 即可完成创建。

{width=”4.998992782152231in” height=”3.4479166666666665in”}

创建完成以后,可以在 IDEA 右边看到创建的 Module。如果没有,可以刷新一下。目录下也会有对应的生命周期。其中常用的是:clean、compile、package、install。

比如这里install,如果其他项目需要将这里的模块作为依赖使用,那就可以install。安装到本地仓库的位置。

{width=”6.71454615048119in” height=”2.1656244531933506in”}

其他操作这里不再赘述。

十三、其它设置

1. 生成 javadoc

{width=”2.9392300962379703in” height=”2.361561679790026in”}

{width=”5.509103237095363in” height=”6.1875in”}

输入:

2. 缓存和索引的清理

IntelliJ IDEA 首次加载项目的时候,都会创建索引,而创建索引的时间跟项目的文件多少成正比。在 IntelliJ IDEA 创建索引过程中即使你编辑了代码也是编译不了、运行不起来的,所以还是安安静静等 IntelliJ IDEA 创建索引完成。

{width=”3.031248906386702in” height=”3.9805555555555556in”}IntelliJ IDEA 的缓存和索引主要是用来加快文件查询,从而加快各种查找、代码提示等操作的速度,所以 IntelliJ IDEA 的索引的重要性再强调一次也不为过。但是,IntelliJ IDEA 的索引和缓存并不是一直会良好地支持 IntelliJ IDEA 的, 某些特殊条件下,IntelliJ IDEA 的缓存和索引文件也是会损坏的,比如:断电、蓝屏引起的强制关机,当你重新打开 IntelliJ IDEA,很可能 IntelliJ IDEA 会报各种莫名其妙错误,甚至项目打不开,IntelliJ IDEA 主题还原成默认状态。即使没有断电、蓝屏,也会有莫名奇怪的问题的时候,也很有可能是 IntelliJ IDEA 缓存和索引出现了问题,这种情况还不少。遇到此类问题也不用过多担心。我们可以清理缓存和索引。如下:

{width=”6.656733377077865in” height=”1.61875in”}

  • 一般建议点击 Invalidate and Restart,这样会比较干净。

  • 上图警告:清除索引和缓存会使得 IntelliJ IDEA 的 Local History 丢失。所以如果你项目没有加入到版本控制,而你又需要你项目文件的历史更改记录,那你最好备份下你的

LocalHistory 目 录 。 目 录 地 址 在 : C:\Users\ 当 前 登 录 的 系 统 用 户 名

\.IntelliJIdea14\system\LocalHistory 建议使用硬盘的全文搜索,这样效率更高。

  • 通过上面方式清除缓存、索引本质也就是去删除 C 盘下的 system 目录下的对应的文件而已,所以如果你不用上述方法也可以删除整个 system。当 IntelliJ IDEA 再次启动项目的时候会重新创建新的 system 目录以及对应项目缓存和索引。

3. 取消更新

{width=”5.9617224409448815in” height=”2.0875in”}

取消勾选:即可取消更新

4. 插件的使用

在 IntelliJ IDEA 的安装讲解中我们其实已经知道,IntelliJ IDEA 本身很多功能也都是通过插件的方式来实现的。

官网插件库:https://plugins.jetbrains.com/

{width=”6.684407261592301in” height=”4.901666666666666in”}

需要特别注意的是:在国内的网络下,经常出现显示不了插件列表,或是显示了插件列表,无法下载完成安装。这时候请自行打开 VPN,一般都可以得到解决。

{width=”5.966307961504812in” height=”3.4546872265966755in”}

{width=”5.958747812773403in” height=”3.8772911198600175in”}

如上图演示,在线安装 IntelliJ IDEA 插件库中的插件。安装完以后会提示重启, 才可以使用插件。

常用插件推荐:

插件名称 插件介绍 官网地址
Key promoter 快捷键提示 https://plugins.jetbrains.com/plugin/4455?pr=idea
CamelCase 驼峰式命名和下划线命名交替变化 https://plugins.jetbrains.com/plugin/7160?pr=idea
CheckStyle-IDEA 代码样式检查 https://plugins.jetbrains.com/plugin/1065?pr=idea
FindBugs-IDEA 代码 Bug 检查 https://plugins.jetbrains.com/plugin/3847?pr=idea
Statistic 代码统计 https://plugins.jetbrains.com/plugin/4509?pr=idea
JRebel Plugin 热部署 https://plugins.jetbrains.com/plugin/?id=4441
CodeGlance 在编辑代码最右侧,显示一块代码小地图 https://plugins.jetbrains.com/plugin/7275?pr=idea
Eclipse Code Formatter 使用 Eclipse 的代码格式化风格,在一个团队中如果公司有规定格式化风格,这个可以使用。 https://plugins.jetbrains.com/plugin/6546?pr=idea
GsonFormat 把 JSON 字符串直接实例化成类 https://plugins.jetbrains.com/plugin/7654?pr=idea

流量复制重放工具goreplay与diffy结合使用

发表于 2019-09-12 更新于 2019-09-20 分类于 工具

goreplay简介

https://github.com/buger/goreplay

https://goreplay.org

GoReplay是一个开源工具,用于捕获实时HTTP流量并将其重放到测试环境中,以便使用真实数据持续测试系统。
GoReplay不是代理,而是监听网络接口上的流量,不需要更改生产基础架构,而是在与服务相同的计算机上运行GoReplay守护程序。

goreplay工作原理

goreplay工作原理

goreplay常见用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1. 简单的 HTTP 流量复制:
gor –input-raw :80 –output-http “http://staging.com”

2.HTTP 流量复制频率控制:
gor –input-tcp :28020 –output-http “http://staging.com|10″

3.HTTP 流量复制缩小:
gor –input-raw :80 –output-tcp “replay.local:28020|10%”

4.HTTP 流量记录到本地文件:
gor –input-raw :80 –output-file requests.gor

5.HTTP 流量回放和压测:
gor –input-file “requests.gor|200%” –output-http “staging.com”

6.HTTP 流量过滤复制:
gor –input-raw :8080 –output-http staging.com –output-http-url-regexp ^www.

7.HTTP指定接口流量复制:
gor --input-raw :80 --http-allow-url '/api/v1' --output-stdout //--output-stdout表示直接在控制台输出

gor参数

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
[root@~]# gor --help
Gor is a simple http traffic replication tool written in Go. Its main goal is to replay traffic from production servers to staging and dev environments.
Project page: https://github.com/buger/gor
Author: <Leonid Bugaev> leonsbox@gmail.com
Current Version: 1.0.0

-copy-buffer-size int
Set the buffer size for an individual request (default 5M) (default 5242880)
-cpuprofile string
write cpu profile to file
-debug verbose //打开debug模式,显示所有接口的流量
Turn on debug output, shows all intercepted traffic. Works only when with verbose flag
-exit-after duration
exit after specified duration
-http-allow-header value //用一个正则表达式来匹配http头部,如果请求的头部没有匹配上,则被拒绝
A regexp to match a specific header against. Requests with non-matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-header api-version:^v1
-http-allow-method value // 类似于一个白名单机制来允许通过的http请求方法,除此之外的方法都被拒绝.
Whitelist of HTTP methods to replay. Anything else will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-method GET --http-allow-method OPTIONS
-http-allow-url value //一个正则表达式用来匹配url, 用来过滤完全匹配的的url,在此之外的都被过滤掉
A regexp to match requests against. Filter get matched against full url with domain. Anything else will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-url ^www.
-http-basic-auth-filter value //匹配认证头重放
A regexp to match the decoded basic auth string against. Requests with non-matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-basic-auth-filter "^customer[0-9].*"
-http-disallow-header value //用一个正则表达式来匹配http头部,匹配到的请求会被拒绝掉
A regexp to match a specific header against. Requests with matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"
-http-disallow-url value //用一个正则表达式来匹配url,如果请求匹配上了,则会被拒绝
A regexp to match requests against. Filter get matched against full url with domain. Anything else will be forwarded:
gor --input-raw :8080 --output-http staging.com --http-disallow-url ^www.
-http-header-limiter value //读取请求,基于FNV32-1A散列来拒绝一定比例的特殊请求
Takes a fraction of requests, consistently taking or rejecting a request based on the FNV32-1A hash of a specific header:
gor --input-raw :8080 --output-http staging.com --http-header-limiter user-id:25%
-http-original-host //在--output-http的输出中,通常gor会使用取代请求的http头,所以应该禁用该选项,保留原始的主机头
Normally gor replaces the Host http header with the host supplied with --output-http. This option disables that behavior, preserving the original Host header.
-http-param-limiter value
Takes a fraction of requests, consistently taking or rejecting a request based on the FNV32-1A hash of a specific GET param:
gor --input-raw :8080 --output-http staging.com --http-param-limiter user_id:25%
-http-pprof :8181
Enable profiling. Starts http server on specified port, exposing special /debug/pprof endpoint. Example: :8181
-http-rewrite-header value
Rewrite the request header based on a mapping:
gor --input-raw :8080 --output-http staging.com --http-rewrite-header Host: (.*).example.com,$1.beta.example.com
-http-rewrite-url value
Rewrite the request url based on a mapping:
gor --input-raw :8080 --output-http staging.com --http-rewrite-url /v1/user/([^\/]+)/ping:/v2/user/$1/ping
-http-set-header value
Inject additional headers to http reqest:
gor --input-raw :8080 --output-http staging.com --http-set-header 'User-Agent: Gor'
-http-set-param value
Set request url param, if param already exists it will be overwritten:
gor --input-raw :8080 --output-http staging.com --http-set-param api_key=1
-input-dummy value
Used for testing outputs. Emits 'Get /' request every 1s
-input-file value //从一个文件中读取请求
Read requests from file:
gor --input-file ./requests.gor --output-http staging.com
-input-file-loop
Loop input files, useful for performance testing.
-input-kafka-host string
Send request and response stats to Kafka:
gor --output-stdout --input-kafka-host '192.168.0.1:9092,192.168.0.2:9092'
-input-kafka-json-format
If turned on, it will assume that messages coming in JSON format rather than GoReplay text format.
-input-kafka-topic string
Send request and response stats to Kafka:
gor --output-stdout --input-kafka-topic 'kafka-log'
-input-raw value
Capture traffic from given port (use RAW sockets and require *sudo* access):
# Capture traffic from 8080 port
gor --input-raw :8080 --output-http staging.com
-input-raw-bpf-filter string
BPF filter to write custom expressions. Can be useful in case of non standard network interfaces like tunneling or SPAN port. Example: --input-raw-bpf-filter 'dst port 80'
-input-raw-buffer-size int
Controls size of the OS buffer (in bytes) which holds packets until they dispatched. Default value depends by system: in Linux around 2MB. If you see big package drop, increase this value.
-input-raw-engine libpcap
Intercept traffic using libpcap (default), and `raw_socket` (default "libpcap")
-input-raw-expire duration
How much it should wait for the last TCP packet, till consider that TCP message complete. (default 2s)
-input-raw-immediate-mode
Set pcap interface to immediate mode.
-input-raw-override-snaplen
Override the capture snaplen to be 64k. Required for some Virtualized environments
-input-raw-realip-header string
If not blank, injects header with given name and real IP value to the request payload. Usually this header should be named: X-Real-IP
-input-raw-timestamp-type string
Possible values: PCAP_TSTAMP_HOST, PCAP_TSTAMP_HOST_LOWPREC, PCAP_TSTAMP_HOST_HIPREC, PCAP_TSTAMP_ADAPTER, PCAP_TSTAMP_ADAPTER_UNSYNCED. This values not supported on all systems, GoReplay will tell you available values of you put wrong one.
-input-raw-track-response
If turned on Gor will track responses in addition to requests, and they will be available to middleware and file output.
-input-tcp value // 用来在多个gor之间流转流量
Used for internal communication between Gor instances. Example:
# Receive requests from other Gor instances on 28020 port, and redirect output to staging
gor --input-tcp :28020 --output-http staging.com
-input-tcp-certificate string
Path to PEM encoded certificate file. Used when TLS turned on.
-input-tcp-certificate-key string
Path to PEM encoded certificate key file. Used when TLS turned on.
-input-tcp-secure
Turn on TLS security. Do not forget to specify certificate and key files.
-memprofile string
write memory profile to this file
-middleware string
Used for modifying traffic using external command
-output-dummy value //用来测试输入,打印出接收的数据.
DEPRECATED: use --output-stdout instead
-output-file value //把进入的请求写入一个文件中
Write incoming requests to file:
gor --input-raw :80 --output-file ./requests.gor
-output-file-append
The flushed chunk is appended to existence file or not.
-output-file-flush-interval duration
Interval for forcing buffer flush to the file, default: 1s. (default 1s)
-output-file-max-size-limit value
Max size of output file, Default: 1TB (default -1)
-output-file-queue-limit int
The length of the chunk queue. Default: 256 (default 256)
-output-file-size-limit value
Size of each chunk. Default: 32mb (default 33554432)
-output-http value //转发进入的请求到一个http地址上
Forwards incoming requests to given http address.
# Redirect all incoming requests to staging.com address
gor --input-raw :80 --output-http http://staging.com
-output-http-compatibility-mode
Use standard Go client, instead of built-in implementation. Can be slower, but more compatible.
-output-http-debug
Enables http debug output.
-output-http-elasticsearch string //把请求和响应状态发送到ElasticSearch
Send request and response stats to ElasticSearch:
gor --input-raw :8080 --output-http staging.com --output-http-elasticsearch 'es_host:api_port/index_name'
-output-http-header --output-http-header
WARNING: --output-http-header DEPRECATED, use `--http-set-header` instead
-output-http-header-filter --output-http-header-filter
WARNING: --output-http-header-filter DEPRECATED, use `--http-allow-header` instead
-output-http-header-hash-filter output-http-header-hash-filter
WARNING: output-http-header-hash-filter DEPRECATED, use `--http-header-hash-limiter` instead
-output-http-method --output-http-method
WARNING: --output-http-method DEPRECATED, use `--http-allow-method` instead
-output-http-queue-len int
Number of requests that can be queued for output, if all workers are busy. default = 1000 (default 1000)
-output-http-redirects int //设置多少次重定向被允许
Enable how often redirects should be followed.
-output-http-response-buffer int
HTTP response buffer size, all data after this size will be discarded.
-output-http-rewrite-url --output-http-rewrite-url
WARNING: --output-http-rewrite-url DEPRECATED, use `--http-rewrite-url` instead
-output-http-stats //每5秒钟输出一次输出队列的状态
Report http output queue stats to console every N milliseconds. See output-http-stats-ms
-output-http-stats-ms int
Report http output queue stats to console every N milliseconds. default: 5000 (default 5000)
-output-http-timeout duration //指定http的request/response超时时间,默认是5秒
Specify HTTP request/response timeout. By default 5s. Example: --output-http-timeout 30s (default 5s)
-output-http-track-response
If turned on, HTTP output responses will be set to all outputs like stdout, file and etc.
-output-http-url-regexp --output-http-url-regexp
WARNING: --output-http-url-regexp DEPRECATED, use `--http-allow-url` instead
-output-http-workers int // gor默认是动态的扩展工作者数量,你也可以指定固定数量的工作者
Gor uses dynamic worker scaling. Enter a number to set a maximum number of workers. default = 0 = unlimited.
-output-http-workers-min int
Gor uses dynamic worker scaling. Enter a number to set a minimum number of workers. default = 1.
-output-kafka-host string
Read request and response stats from Kafka:
gor --input-raw :8080 --output-kafka-host '192.168.0.1:9092,192.168.0.2:9092'
-output-kafka-json-format
If turned on, it will serialize messages from GoReplay text format to JSON.
-output-kafka-topic string
Read request and response stats from Kafka:
gor --input-raw :8080 --output-kafka-topic 'kafka-log'
-output-null
Used for testing inputs. Drops all requests.
-output-stdout
Used for testing inputs. Just prints to console data coming from inputs.
-output-tcp value //用来在多个gor之间流转流量
Used for internal communication between Gor instances. Example:
# Listen for requests on 80 port and forward them to other Gor instance on 28020 port
gor --input-raw :80 --output-tcp replay.local:28020
-output-tcp-secure
Use TLS secure connection. --input-file on another end should have TLS turned on as well.
-output-tcp-stats //每5秒钟报告一次tcp输出队列的状态
Report TCP output queue stats to console every 5 seconds.
-prettify-http
If enabled, will automatically decode requests and responses with: Content-Encodning: gzip and Transfer-Encoding: chunked. Useful for debugging, in conjuction with --output-stdout
-split-output true
By default each output gets same traffic. If set to true it splits traffic equally among all outputs.
-stats //打开输出队列的状态
Turn on queue stats output
-verbose
Turn on more verbose output

diffy简介

http://www.github.com/twitter/diffy

Diffy是一个开源的自动化测试工具,它能够自动检测基于Apache Thrift或者基于HTTP的服务。使用Diffy,只需要进行简单的配置,之后不需要再编写测试代码。
Diffy主要基于稳定版本和它的副本的输出,对候选版本的输出进行比较,以检查候选版本是否正确。因此,Diffy首先假设候选版本应该和稳定版本有“相似”的输出。即不论候选版本和稳定版本系统模块是否相同,他们的最终输出应该是“相似”的。

Diffy工作原理

在测试过程中,Diffy充当一个代理,它能够将来源请求分发到不同版本的系统中去,通过对各个版本系统的输出进行对比,做出最终的结论。
Diffy需要三个版本的系统,以实现它的噪声过滤和对比功能,它们分别是:
候选版本:该版本是待测版本,相对于生产环境版本有着跟新的代码
稳定版本:该版本通常是已经上线版本,或者是已知功能正常的版本
稳定版本副本:该版本是稳定版本的副本,和稳定版本运行相同的代码,主要用于排除噪声
整个运行流程为:
diffy运行流程

其中:
1.原始区别为候选版本和稳定版本之间输出的区别,其中可能会包含上述的噪声
2.噪声从稳定版本和其副本中获得,如果两个运行相同代码的系统输入相同输出却不同,则Diffy会认为这是开发人员不需要关心的噪声。

基于上述两个区别集合,Diffy可以识别出候选版本和稳定版本真实的区别,这些区别很有可能就是一个缺陷。
当然,对于一个概率性出现随机值,仅仅一次请求的结论可能是不准确的。例如对于一个50%概率出现true或者false的布尔值,则有50%的概率会出现候选版本和稳定版本的不同,同时又会有50%的概率出现稳定版本和其副本出现不同(即将这个值认定为噪声),最终会有25%的概率认为这是一个缺陷。因为此时稳定版本和其副本值相同,候选版本和稳定版本值不同。因此,Diffy还会聚合原始区别和噪声,当发现二者出现的概率类似的时候,会认定之前识别出来的缺陷属于误报。

示例

gor

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
https://github.com/buger/goreplay/wiki/Getting-Started

#捕获网络流量
1. 模拟启动一个server
gor file-server :8000
2. 抓取8000端口流量
gor --input-raw :8000 --output-stdout
3. 浏览器或curl模拟请求,两个终端窗口都会打印输出
http://localhost:8000


#流量重放
4. 启动另外一个server,模拟流量重放
gor file-server :8001
5. 流量复制到另外一个server
gor --input-raw :8000 --output-http="http://localhost:8001"


gor --input-raw :8000 --output-http="http://localhost:8001|50%"
6. 浏览器或curl模拟请求,8000/8001端口都会输出响应
http://localhost:8000


#将请求保存到文件并稍后重放
7. 将请求记录到文件
gor --input-raw :8000 --output-file=requests.gor
实际会分批保存为 request_0.gor,request_1.gor 这种文件名。
236K requests_0.gor
236K requests_10.gor
236K requests_11.gor
240K requests_12.gor
236K requests_13.gor
236K requests_14.gor
188K requests_15.gor
236K requests_1.gor
240K requests_2.gor
236K requests_3.gor
240K requests_4.gor
236K requests_5.gor
240K requests_6.gor
236K requests_7.gor
236K requests_8.gor
236K requests_9.gor
8. 浏览器或curl模拟请求
http://localhost:8000
9. 流量回放,请求全部会转发到8001端口
gor --input-file requests_0.gor --output-http="http://localhost:8001"

gor限流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Limiting replay using absolute number
# staging.server will not get more than ten requests per second
gor --input-tcp :28020 --output-http "http://staging.com|10"


# Limiting listener using percentage based limiter
# replay server will not get more than 10% of requests
# useful for high-load environments
gor --input-raw :80 --output-tcp "replay.local:28020|10%"


# Consistent limiting based on Header or URL param value
# Limit based on header value
gor --input-raw :80 --output-tcp "replay.local:28020|10%" --http-header-limiter "X-API-KEY: 10%"
# Limit based on header value
gor --input-raw :80 --output-tcp "replay.local:28020|10%" --http-param-limiter "api_key: 10%"

# Performance testing
# Replay from file on 2x speed
gor --input-file "requests.gor|200%" --output-http "staging.com"

goreplay+diffy

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
1. Deploy your old code to localhost:9990. This is your primary.
nohup java -Dserver.port=9990 -jar s1.jar &

2. Deploy your old code to localhost:9991. This is your secondary.
nohup java -Dserver.port=9991 -jar s1.jar &

3. Deploy your new code to localhost:9992. This is your candidate.
nohup java -Dserver.port=9992 -jar s1.jar &

4. Download the latest Diffy binary from maven central or build your own from the code using ./sbt assembly.

5. Run the Diffy jar with following command line arguments:
java -jar diffy-server.jar \
-candidate=localhost:9992 \
-master.primary=localhost:9990 \
-master.secondary=localhost:9991 \
-service.protocol=http \
-serviceName=My-Service \
-proxy.port=:8880 \
-admin.port=:8881 \
-http.port=:8888 \
-rootUrl='localhost:8888'

注:此处需要下载源码重新编译打包成diffy-server.jar

java -jar diffy-server.jar -candidate=localhost:9992 -master.primary=localhost:9990 -master.secondary=localhost:9991 -service.protocol=http -serviceName=My-Service -proxy.port=:8880 -admin.port=:8881 -http.port=:8888 -rootUrl='localhost:8888'

步骤1-5运行完毕后,进程:
root 3952 1 0 14:12 ? 00:00:17 java -Dserver.port=9990 -jar s1.jar
root 4004 1 0 14:13 ? 00:00:15 java -Dserver.port=9991 -jar s1.jar
root 4049 1 0 14:13 ? 00:00:15 java -Dserver.port=9992 -jar s1.jar
root 16863 2113 99 14:58 pts/0 00:00:05 java -jar diffy-server.jar -candidate=localhost:9992 -master.primary=localhost:9990 -master.secondary=localhost:9991 -service.protocol=http -serviceName=My-Service -proxy.port=:8880 -admin.port=:8881 -http.port=:8888 -rootUrl=localhost:8888


6. Send a few test requests to your Diffy instance on its proxy port:
curl localhost:8880/your/application/route?with=queryparams

注:此处是代理端口,如果使用goreplay复制流量时,将指向该代理端口
gor --input-raw :8000 --output-http="http://diffserver:{diff proxy port}"

7. Watch the differences show up in your browser at http://localhost:8888.

参考

1
2
3
4
5
6
7
8
https://goreplay.org/
https://github.com/buger/gor/wiki
https://github.com/twitter/diffy/

https://www.cnblogs.com/playboysnow/articles/9759366.html
https://studygolang.com/articles/10205
http://tyrion.iteye.com/blog/2311987
https://blog.51cto.com/xqtesting/2068569

spring-retry

发表于 2019-09-05 更新于 2019-09-12 分类于 spring

springboot集成spring-retry框架

1.加入依赖

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>

2.在主类上加上@EnableRetry注解,表示启用重试机制

1
2
3
4
5
6
7
@SpringBootApplication
@EnableRetry
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}

3.定义一个简单的controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
public class HelloController {
Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private PayService payService;

@GetMapping("/createOrder")
public String createOrder(@RequestParam int num) throws Exception{
int remainingnum = payService.minGoodsnum(num == 0 ? 1: num);
logger.info("剩余的数量==="+remainingnum);
return "库库存成功";
}

}

4.在controller中调用减库存的service接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Service
public class PayService {
private Logger logger = LoggerFactory.getLogger(getClass());
private final int totalNum = 100000;

@Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))
public int minGoodsnum(int num) throws Exception{
logger.info("minGoodsnum开始"+ LocalTime.now());
if(num <= 0){
throw new Exception("数量不对");
}
logger.info("minGoodsnum执行结束");
return totalNum - num;
}
}

注解及参数说明

1
2
3
4
5
6
7
8
9
10
11
12
注解说明:
@Retryable:标注此注解的方法在发生异常时会进行重试
@Recover:用于@Retryable重试失败后处理方法,此方法里的异常一定要是@Retryable方法里抛出的异常,否则不会调用这个方法
参数说明:
value: 抛出指定异常才会重试,可以抛出该异常子类
include:和value一样,默认为空,当exclude也为空时,默认所有异常
exclude:指定不处理的异常
maxAttempts:最大重试次数,默认3次
backoff:重试等待策略,默认使用@Backoff,@Backoff的value默认为1000L,multiplier(指定延迟倍数,默认为0,表示固定暂停1秒后进行重试,如果把multiplier设置为2,则第一次重试为1秒,第二次为2秒,第三次为4秒)
delay表示重试的延迟时间:单位为毫秒,默认1000(1秒)
multiplier表示下一次延时时间是这一次的倍数,默认为0,表示忽略
比如第一次延迟时间为2s,multiplier为1.5时,第二次延迟时间为2*1.5=3s,第三次延迟时间为3*1.5=4.5s

5.默认情况重试三次抛出异常,可使用@Recover注解,当重试次数达到设置的次数的时候,执行的回调函数。(此时不会再抛出异常)

1
2
3
4
5
6
7
和minGoodsnum定义在一个类中
@Recover
public int recover(Exception e) {
logger.warn("减库存失败!!!" + LocalTime.now() + e.getMessage());
//记日志到数据库
return totalNum;
}

6.测试

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
a.采用http接口
@Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000, multiplier = 1.5))

正常情况:http://localhost:8080/createOrder?num=10
减库存开始13:53:42.470
减库存执行结束13:53:42.471
剩余的数量===99990

异常情况: http://localhost:8080/createOrder?num=-1
减库存开始13:54:09.691
减库存开始13:54:11.691
减库存开始13:54:14.693
减库存开始13:54:19.194
减库存开始13:54:25.944
减库存失败!!!13:54:25.944数量不对
剩余的数量===99990



@Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000))
正常情况:http://localhost:8080/createOrder?num=10
减库存开始14:03:02.009
减库存执行结束14:03:02.009
剩余的数量===99990

异常情况: http://localhost:8080/createOrder?num=-1
减库存开始14:03:05.217
减库存开始14:03:07.218
减库存开始14:03:09.218
减库存开始14:03:11.218
减库存开始14:03:13.227
减库存失败!!!14:03:13.228数量不对
剩余的数量===99990


b.采用junit测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRetryApplicationTests {

@Autowired
private PayService payService;

@Test
public void payTest() throws Exception {
int store = payService.minGoodsnum(-1);
System.out.println("库存为:" + store);
}

}

减库存开始14:18:09.657
减库存开始14:18:11.659
减库存开始14:18:13.660
减库存开始14:18:15.661
减库存开始14:18:17.662
减库存失败!!!14:18:17.666数量不对

Java线程池-ThreadPoolExecutor原理分析

发表于 2019-08-28 更新于 2019-09-12 分类于 并发编程

一、线程池初始化

根据阿里巴巴开发者手册规范,线程池应通过ThreadPoolExecutor来创建,避免产生OOM
阿里巴巴线程池使用规范

在 java.util.concurrent 包中,提供了 ThreadPoolExecutor 的实现。

1
2
3
4
5
6
7
8
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
}

参数详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
corePoolSize - 线程池中核心线程数大小,需要注意的是在初创建线程池时线程不会立即启动,直到有任务提交才开始启动线程并逐渐时线程数目达到corePoolSize。若想一开始就创建所有核心线程需调用prestartAllCoreThreads方法。

maximumPoolSize - 线程池中允许创建的最大线程数。需要注意的是当核心线程满且阻塞队列也满时才会判断当前线程数是否小于最大线程数,并决定是否创建新线程。

keepAliveTime - 当线程数大于核心时,多于的空闲线程最多存活时间

unit - keepAliveTime 参数的时间单位。

workQueue - 当线程数目超过核心线程数时用于保存任务的队列。主要有3种类型的BlockingQueue可供选择:无界队列,有界队列和同步移交。将在下文中详细阐述。从参数中可以看到,此队列仅保存实现Runnable接口的任务。

threadFactory - 执行程序创建新线程时使用的工厂。

handler - 阻塞队列已满且线程数达到最大值时所采取的饱和策略。java默认提供了4种饱和策略的实现方式:中止、抛弃、抛弃最旧的、调用者运行。将在下文中详细阐述。

总结:
1 如果当前运行的线程少于corePoolSize,则创建新线程来执行任务(需要获得全局锁)
2 如果运行的线程等于或多于corePoolSize ,则将任务加入BlockingQueue
3 如果无法将任务加入BlockingQueue(队列已满),则创建新的线程来处理任务(需要获得全局锁)
4 如果创建新线程将使当前运行的线程超出maxiumPoolSize,任务将被拒绝,并调用RejectedExecutionHandler.rejectedExecution()方法。
线程池采取上述的流程进行设计是为了减少获取全局锁的次数。在线程池完成预热(当前运行的线程数大于或等于corePoolSize)之后,几乎所有的excute方法调用都执行步骤2。

二、线程池的处理流程

向线程池提交一个任务后,它的主要处理流程如下图所示
线程池处理流程

一个线程从被提交(submit)到执行共经历以下流程:

1
2
3
1.线程池判断核心线程池里是的线程是否都在执行任务,如果不是,则创建一个新的工作线程来执行任务。如果核心线程池里的线程都在执行任务,则进入下一个流程。
2.线程池判断工作队列是否已满。如果工作队列没有满,则将新提交的任务储存在这个工作队列里。如果工作队列满了,则进入下一个流程。
3.线程池判断其内部线程是否都处于工作状态。如果没有,则创建一个新的工作线程来执行任务。如果已满了,则交给饱和策略来处理这个任务。

三、可选择的阻塞队列BlockingQueue

再重复一下新任务进入时线程池的执行策略:

1
2
3
1.如果运行的线程少于corePoolSize,则 Executor始终首选添加新的线程,而不进行排队。(如果当前运行的线程小于corePoolSize,则任务根本不会存入queue中,而是直接运行) 
2.如果运行的线程大于等于 corePoolSize,则 Executor始终首选将请求加入队列,而不添加新的线程。
3.如果无法将请求加入队列,则创建新的线程,除非创建此线程超出 maximumPoolSize,在这种情况下,任务将被拒绝。

主要有3种类型的BlockingQueue:

无界队列

队列大小无限制,常用的为无界的LinkedBlockingQueue,使用该队列做为阻塞队列时要尤其当心,当任务耗时较长时可能会导致大量新任务在队列中堆积最终导致OOM。阅读代码发现,Executors.newFixedThreadPool 采用就是 LinkedBlockingQueue,而楼主踩到的就是这个坑,当QPS很高,发送数据很大,大量的任务被添加到这个无界LinkedBlockingQueue 中,导致cpu和内存飙升服务器挂掉。

有界队列

常用的有两类,一类是遵循FIFO原则的队列如ArrayBlockingQueue与有界的LinkedBlockingQueue,另一类是优先级队列如PriorityBlockingQueue。PriorityBlockingQueue中的优先级由任务的Comparator决定。
使用有界队列时队列大小需和线程池大小互相配合,线程池较小有界队列较大时可减少内存消耗,降低cpu使用率和上下文切换,但是可能会限制系统吞吐量。

同步移交队列

如果不希望任务在队列中等待而是希望将任务直接移交给工作线程,可使用SynchronousQueue作为等待队列。SynchronousQueue不是一个真正的队列,而是一种线程之间移交的机制。要将一个元素放入SynchronousQueue中,必须有另一个线程正在等待接收这个元素。只有在使用无界线程池或者有饱和策略时才建议使用该队列。

四、可选择的饱和策略RejectedExecutionHandler

JDK主要提供了4种饱和策略供选择。4种策略都做为静态内部类在ThreadPoolExcutor中进行实现。

AbortPolicy中止策略

该策略是默认饱和策略。

1
2
3
4
5
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}

使用该策略时在饱和时会抛出RejectedExecutionException(继承自RuntimeException),调用者可捕获该异常自行处理。

DiscardPolicy抛弃策略

1
2
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}

如代码所示,不做任何处理直接抛弃任务

DiscardOldestPolicy抛弃旧任务策略

1
2
3
4
5
6
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}

如代码,先将阻塞队列中的头元素出队抛弃,再尝试提交任务。如果此时阻塞队列使用PriorityBlockingQueue优先级队列,将会导致优先级最高的任务被抛弃,因此不建议将该种策略配合优先级队列使用。

CallerRunsPolicy调用者运行

1
2
3
4
5
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}

既不抛弃任务也不抛出异常,直接运行任务的run方法,换言之将任务回退给调用者来直接运行。使用该策略时线程池饱和后将由调用线程池的主线程自己来执行任务,因此在执行任务的这段时间里主线程无法再提交新任务,从而使线程池中工作线程有时间将正在处理的任务处理完成。

五、Java提供的四种常用线程池解析

简而言之:
Executors工厂方法Executors.newCachedThreadPool() 提供了无界线程池,可以进行自动线程回收;
Executors.newFixedThreadPool(int) 提供了固定大小线程池,内部使用无界队列;
Executors.newSingleThreadExecutor() 提供了单个后台线程。

详细介绍一下上述四种线程池。

newCachedThreadPool

1
2
3
4
5
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}

在newCachedThreadPool中如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
初看该构造函数时我有这样的疑惑:核心线程池为0,那按照前面所讲的线程池策略新任务来临时无法进入核心线程池,只能进入 SynchronousQueue中进行等待,而SynchronousQueue的大小为1,那岂不是第一个任务到达时只能等待在队列中,直到第二个任务到达发现无法进入队列才能创建第一个线程?
这个问题的答案在上面讲SynchronousQueue时其实已经给出了,要将一个元素放入SynchronousQueue中,必须有另一个线程正在等待接收这个元素。因此即便SynchronousQueue一开始为空且大小为1,第一个任务也无法放入其中,因为没有线程在等待从SynchronousQueue中取走元素。因此第一个任务到达时便会创建一个新线程执行该任务。

newFixedThreadPool

1
2
3
4
5
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}

看代码一目了然了,线程数量固定,使用无限大的队列。

newScheduledThreadPool

创建一个定长线程池,支持定时及周期性任务执行。

1
2
3
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}

在来看看ScheduledThreadPoolExecutor()的构造函数

1
2
3
4
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}

ScheduledThreadPoolExecutor的父类即ThreadPoolExecutor,因此这里各参数含义和上面一样。值得关心的是DelayedWorkQueue这个阻塞对列,在上面没有介绍,它作为静态内部类就在ScheduledThreadPoolExecutor中进行了实现。简单的说,DelayedWorkQueue是一个无界队列,它能按一定的顺序对工作队列中的元素进行排列。

newSingleThreadExecutor

创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

1
2
3
4
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}

首先new了一个线程数目为 1 的ScheduledThreadPoolExecutor,再把该对象传入DelegatedScheduledExecutorService中,看看DelegatedScheduledExecutorService的实现代码:

1
2
3
4
DelegatedScheduledExecutorService(ScheduledExecutorService executor) {
super(executor);
e = executor;
}

在看看它的父类

1
2
3
DelegatedExecutorService(ExecutorService executor) { 
e = executor;
}

其实就是使用装饰模式增强了ScheduledExecutorService(1)的功能,不仅确保只有一个线程顺序执行任务,也保证线程意外终止后会重新创建一个线程继续执行任务。

参考

https://zhuanlan.zhihu.com/p/32867181
https://juejin.im/post/5bd906046fb9a05d3c80496b
https://mp.weixin.qq.com/s/-89-CcDnSLBYy3THmcLEdQ
https://www.jianshu.com/p/519803f392dc

SpringBoot笔记

发表于 2019-07-29 更新于 2019-09-12 分类于 springboot

一、Spring Boot 入门

1、Spring Boot 简介

简化Spring应用开发的一个框架;

整个Spring技术栈的一个大整合;

J2EE开发的一站式解决方案;

2、微服务

2014,martin fowler

微服务:架构风格(服务微化)

一个应用应该是一组小型服务;可以通过HTTP的方式进行互通;

单体应用:ALL IN ONE

微服务:每一个功能元素最终都是一个可独立替换和独立升级的软件单元;

详细参照微服务文档

3、环境准备

http://www.gulixueyuan.com/ 谷粒学院

环境约束

–jdk1.8:Spring Boot 推荐jdk1.7及以上;java version “1.8.0_112”

–maven3.x:maven 3.3以上版本;Apache Maven 3.3.9

–IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64、STS

–SpringBoot 1.5.9.RELEASE:1.5.9;

统一环境;

1、MAVEN设置;

给maven 的settings.xml配置文件的profiles标签添加

1
2
3
4
5
6
7
8
9
10
11
12
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>

2、IDEA设置

整合maven进来;

idea设置

images/

4、Spring Boot HelloWorld

一个功能:

浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串;

1、创建一个maven工程;(jar)

2、导入spring boot相关的依赖

1
2
3
4
5
6
7
8
9
10
11
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

3、编写一个主程序;启动Spring Boot应用

1
2
3
4
5
6
7
8
9
10
11
12
13

/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {

public static void main(String[] args) {

// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}

4、编写相关的Controller、Service

1
2
3
4
5
6
7
8
9
@Controller
public class HelloController {

@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}

5、运行主程序测试

6、简化部署

1
2
3
4
5
6
7
8
9
<!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

将这个应用打成jar包,直接使用java -jar的命令进行执行;

5、Hello World探究

1、POM文件

1、父项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>

他的父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
他来真正管理Spring Boot应用里面的所有依赖版本;

Spring Boot的版本仲裁中心;

以后我们导入依赖默认是不需要写版本;(没有在dependencies里面管理的依赖自然需要声明版本号)

2、启动器

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter-==web==:

​ spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件;

Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器

2、主程序类,主入口类

1
2
3
4
5
6
7
8
9
10
11
12
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {

public static void main(String[] args) {

// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}

@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;

1
2
3
4
5
6
7
8
9
10
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

@SpringBootConfiguration:Spring Boot的配置类;

​ 标注在某个类上,表示这是一个Spring Boot的配置类;

​ @Configuration:配置类上来标注这个注解;

​ 配置类 —– 配置文件;配置类也是容器中的一个组件;@Component

@EnableAutoConfiguration:开启自动配置功能;

​ 以前我们需要配置的东西,Spring Boot帮我们自动配置;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生效;

1
2
3
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

​ @AutoConfigurationPackage:自动配置包

​ @Import(AutoConfigurationPackages.Registrar.class):

​ Spring的底层注解@Import,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class;

==将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;==

​ @Import(EnableAutoConfigurationImportSelector.class);

​ 给容器中导入组件?

​ EnableAutoConfigurationImportSelector:导入哪些组件的选择器;

​ 将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;

​ 会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件; 自动配置类

有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;

​ SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);

==Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;==以前我们需要自己配置的东西,自动配置类都帮我们;

J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;

​

==Spring注解版(谷粒学院)==

6、使用Spring Initializer快速创建Spring Boot项目

1、IDEA:使用 Spring Initializer快速创建项目

IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目;

选择我们需要的模块;向导会联网创建Spring Boot项目;

默认生成的Spring Boot项目;

  • 主程序已经生成好了,我们只需要我们自己的逻辑
  • resources文件夹中目录结构
    • static:保存所有的静态资源; js css images;
    • templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
    • application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;

2、STS使用 Spring Starter Project快速创建项目


二、配置文件

1、配置文件

SpringBoot使用一个全局的配置文件,配置文件名是固定的;

•application.properties

•application.yml

配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;

YAML(YAML Ain’t Markup Language)

​ YAML A Markup Language:是一个标记语言

​ YAML isn’t Markup Language:不是一个标记语言;

标记语言:

​ 以前的配置文件;大多都使用的是 xxxx.xml文件;

​ YAML:以数据为中心,比json、xml等更适合做配置文件;

​ YAML:配置例子

1
2
server:
port: 8081

​ XML:

1
2
3
<server>
<port>8081</port>
</server>

2、YAML语法:

1、基本语法

k:(空格)v:表示一对键值对(空格必须有);

以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的

1
2
3
server:
port: 8081
path: /hello

属性和值也是大小写敏感;

2、值的写法

字面量:普通的值(数字,字符串,布尔)

​ k: v:字面直接来写;

​ 字符串默认不用加上单引号或者双引号;

​ “”:双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思

​ name: “zhangsan \n lisi”:输出;zhangsan 换行 lisi

​ ‘’:单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据

​ name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi

对象、Map(属性和值)(键值对):

​ k: v:在下一行来写对象的属性和值的关系;注意缩进

​ 对象还是k: v的方式

1
2
3
friends:
lastName: zhangsan
age: 20

行内写法:

1
friends: {lastName: zhangsan,age: 18}

数组(List、Set):

用- 值表示数组中的一个元素

1
2
3
4
pets:
- cat
- dog
- pig

行内写法

1
pets: [cat,dog,pig]

3、配置文件值注入

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 12

javaBean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
* prefix = "person":配置文件中哪个下面的所有属性进行一一映射
*
* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
*
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

private String lastName;
private Integer age;
private Boolean boss;
private Date birth;

private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;

我们可以导入配置文件处理器,以后编写配置就有提示了

1
2
3
4
5
6
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

1、properties配置文件在idea中默认utf-8可能会乱码

调整

idea配置乱码

2、@Value获取值和@ConfigurationProperties获取值比较

@ConfigurationProperties @Value
功能 批量注入配置文件中的属性 一个个指定
松散绑定(松散语法) 支持 不支持
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持

配置文件yml还是properties他们都能获取到值;

如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;

如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

3、配置文件注入值数据校验

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

/**
* <bean class="Person">
* <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
* <bean/>
*/

//lastName必须是邮箱格式
@Email
//@Value("${person.last-name}")
private String lastName;
//@Value("#{11*2}")
private Integer age;
//@Value("true")
private Boolean boss;

private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;

4、@PropertySource&@ImportResource&@Bean

@PropertySource:加载指定的配置文件;

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
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
* prefix = "person":配置文件中哪个下面的所有属性进行一一映射
*
* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
* @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
*
*/
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {

/**
* <bean class="Person">
* <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
* <bean/>
*/

//lastName必须是邮箱格式
// @Email
//@Value("${person.last-name}")
private String lastName;
//@Value("#{11*2}")
private Integer age;
//@Value("true")
private Boolean boss;

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;

想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

1
2
@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效

不来编写Spring的配置文件

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
</beans>

SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式

1、配置类@Configuration——>Spring配置文件

2、使用@Bean给容器中添加组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
*
* 在配置文件中用<bean><bean/>标签添加组件
*
*/
@Configuration
public class MyAppConfig {

//将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
@Bean
public HelloService helloService02(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new HelloService();
}
}

##4、配置文件占位符

1、随机数

1
2
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}

2、占位符获取之前配置的值,如果没有可以是用:指定默认值

1
2
3
4
5
6
7
8
9
person.last-name=张三${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog
person.dog.age=15

5、Profile

1、多Profile文件

我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml

默认使用application.properties的配置;

2、yml支持多文档块方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

server:
port: 8081
spring:
profiles:
active: prod

---
server:
port: 8083
spring:
profiles: dev


---

server:
port: 8084
spring:
profiles: prod #指定属于哪个环境

3、激活指定profile

​ 1、在配置文件中指定 spring.profiles.active=dev

​ 2、命令行:

​ java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar –spring.profiles.active=dev;

​ 可以直接在测试的时候,配置传入命令行参数

​ 3、虚拟机参数;

​ -Dspring.profiles.active=dev

6、配置文件加载位置

springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件

–file:./config/

–file:./

–classpath:/config/

–classpath:/

优先级由高到底,高优先级的配置会覆盖低优先级的配置;

SpringBoot会从这四个位置全部加载主配置文件;互补配置;

==我们还可以通过spring.config.location来改变默认的配置文件位置==

项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar –spring.config.location=G:/application.properties

7、外部配置加载顺序

==SpringBoot也可以从以下位置加载配置; 优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置==

1.命令行参数

所有的配置都可以在命令行上进行指定

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar –server.port=8087 –server.context-path=/abc

多个配置用空格分开; –配置项=值

2.来自java:comp/env的JNDI属性

3.Java系统属性(System.getProperties())

4.操作系统环境变量

5.RandomValuePropertySource配置的random.*属性值

==由jar包外向jar包内进行寻找;==

==优先加载带profile==

6.jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件

7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件

==再来加载不带profile==

8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件

9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件

10.@Configuration注解类上的@PropertySource

11.通过SpringApplication.setDefaultProperties指定的默认属性

所有支持的配置加载来源;

参考官方文档

8、自动配置原理

配置文件到底能写什么?怎么写?自动配置原理;

配置文件能配置的属性参照

1、自动配置原理:

1)、SpringBoot启动的时候加载主配置类,开启了自动配置功能 ==@EnableAutoConfiguration==

2)、@EnableAutoConfiguration 作用:

  • 利用EnableAutoConfigurationImportSelector给容器中导入一些组件?
  • 可以查看selectImports()方法的内容;

  • List configurations = getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置

    • 1
      2
      3
      4
      SpringFactoriesLoader.loadFactoryNames()
      扫描所有jar包类路径下 META-INF/spring.factories
      把扫描到的这些文件的内容包装成properties对象
      从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中

      ​

==将 类路径下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中;==

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

每一个这样的 xxxAutoConfiguration类都是容器中的一个组件,都加入到容器中;用他们来做自动配置;

3)、每一个自动配置类进行自动配置功能;

4)、以HttpEncodingAutoConfiguration(Http编码自动配置)为例解释自动配置原理;

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
@Configuration   //表示这是一个配置类,以前编写的配置文件一样,也可以给容器中添加组件
@EnableConfigurationProperties(HttpEncodingProperties.class) //启动指定类的ConfigurationProperties功能;将配置文件中对应的值和HttpEncodingProperties绑定起来;并把HttpEncodingProperties加入到ioc容器中

@ConditionalOnWebApplication //Spring底层@Conditional注解(Spring注解版),根据不同的条件,如果满足指定的条件,整个配置类里面的配置就会生效; 判断当前应用是否是web应用,如果是,当前配置类生效

@ConditionalOnClass(CharacterEncodingFilter.class) //判断当前项目有没有这个类CharacterEncodingFilter;SpringMVC中进行乱码解决的过滤器;

@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) //判断配置文件中是否存在某个配置 spring.http.encoding.enabled;如果不存在,判断也是成立的
//即使我们配置文件中不配置pring.http.encoding.enabled=true,也是默认生效的;
public class HttpEncodingAutoConfiguration {

//他已经和SpringBoot的配置文件映射了
private final HttpEncodingProperties properties;

//只有一个有参构造器的情况下,参数的值就会从容器中拿
public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
this.properties = properties;
}

@Bean //给容器中添加一个组件,这个组件的某些值需要从properties中获取
@ConditionalOnMissingBean(CharacterEncodingFilter.class) //判断容器没有这个组件?
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
return filter;
}

根据当前不同的条件判断,决定这个配置类是否生效?

一但这个配置类生效;这个配置类就会给容器中添加各种组件;这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的;

5)、所有在配置文件中能配置的属性都是在xxxxProperties类中封装者‘;配置文件能配置什么就可以参照某个功能对应的这个属性类

1
2
3
4
@ConfigurationProperties(prefix = "spring.http.encoding")  //从配置文件中获取指定的值和bean的属性进行绑定
public class HttpEncodingProperties {

public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

精髓:

​ 1)、SpringBoot启动会加载大量的自动配置类

​ 2)、我们看我们需要的功能有没有SpringBoot默认写好的自动配置类;

​ 3)、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件有,我们就不需要再来配置了)

​ 4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这些属性的值;

xxxxAutoConfigurartion:自动配置类;

给容器中添加组件

xxxxProperties:封装配置文件中相关属性;

2、细节

1、@Conditional派生注解(Spring注解版原生的@Conditional作用)

作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效;

@Conditional扩展注解 作用(判断是否满足当前指定条件)
@ConditionalOnJava 系统的java版本是否符合要求
@ConditionalOnBean 容器中存在指定Bean;
@ConditionalOnMissingBean 容器中不存在指定Bean;
@ConditionalOnExpression 满足SpEL表达式指定
@ConditionalOnClass 系统中有指定的类
@ConditionalOnMissingClass 系统中没有指定的类
@ConditionalOnSingleCandidate 容器中只有一个指定的Bean,或者这个Bean是首选Bean
@ConditionalOnProperty 系统中指定的属性是否有指定的值
@ConditionalOnResource 类路径下是否存在指定资源文件
@ConditionalOnWebApplication 当前是web环境
@ConditionalOnNotWebApplication 当前不是web环境
@ConditionalOnJndi JNDI存在指定项

自动配置类必须在一定的条件下才能生效;

我们怎么知道哪些自动配置类生效;

==我们可以通过启用 debug=true属性;来让控制台打印自动配置报告==,这样我们就可以很方便的知道哪些自动配置类生效;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
=========================
AUTO-CONFIGURATION REPORT
=========================


Positive matches:(自动配置类启用的)
-----------------

DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition)


Negative matches:(没有启动,没有匹配成功的自动配置类)
-----------------

ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)

AopAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)

三、日志

1、日志框架

小张;开发一个大型系统;

​ 1、System.out.println(“”);将关键数据打印在控制台;去掉?写在一个文件?

​ 2、框架来记录系统的一些运行时信息;日志框架 ; zhanglogging.jar;

​ 3、高大上的几个功能?异步模式?自动归档?xxxx? zhanglogging-good.jar?

​ 4、将以前框架卸下来?换上新的框架,重新修改之前相关的API;zhanglogging-prefect.jar;

​ 5、JDBC—数据库驱动;

​ 写了一个统一的接口层;日志门面(日志的一个抽象层);logging-abstract.jar;

​ 给项目中导入具体的日志实现就行了;我们之前的日志框架都是实现的抽象层;

市面上的日志框架;

JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j….

日志门面 (日志的抽象层) 日志实现
JCL(Jakarta Commons Logging) SLF4j(Simple Logging Facade for Java) jboss-logging Log4j JUL(java.util.logging) Log4j2 Logback

左边选一个门面(抽象层)、右边来选一个实现;

日志门面: SLF4J;

日志实现:Logback;

SpringBoot:底层是Spring框架,Spring框架默认是用JCL;‘

​ ==SpringBoot选用 SLF4j和logback;==

2、SLF4j使用

1、如何在系统中使用SLF4j https://www.slf4j.org

以后开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法;

给系统里面导入slf4j的jar和 logback的实现jar

1
2
3
4
5
6
7
8
9
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");
}
}

图示;

images/concrete-bindings.png

每一个日志的实现框架都有自己的配置文件。使用slf4j以后,配置文件还是做成日志实现框架自己本身的配置文件;

2、遗留问题

a(slf4j+logback): Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx

统一日志记录,即使是别的框架和我一起统一使用slf4j进行输出?

如何让系统中所有的日志都统一到slf4j;

==1、将系统中其他日志框架先排除出去;==

==2、用中间包来替换原有的日志框架;==

==3、我们导入slf4j其他的实现==

3、SpringBoot日志关系

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

SpringBoot使用它来做日志功能;

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>

底层依赖关系

总结:

​ 1)、SpringBoot底层也是使用slf4j+logback的方式进行日志记录

​ 2)、SpringBoot也把其他的日志都替换成了slf4j;

​ 3)、中间替换包?

1
2
3
4
5
6
@SuppressWarnings("rawtypes")
public abstract class LogFactory {

static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J = "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";

static LogFactory logFactory = new SLF4JLogFactory();

​ 4)、如果我们要引入其他框架?一定要把这个框架的默认日志依赖移除掉?

​ Spring框架用的是commons-logging;

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

==SpringBoot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉即可;==

4、日志使用;

1、默认配置

SpringBoot默认帮我们配置好了日志;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//记录器
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
//System.out.println();

//日志的级别;
//由低到高 trace<debug<info<warn<error
//可以调整输出的日志级别;日志就只会在这个级别以以后的高级别生效
logger.trace("这是trace日志...");
logger.debug("这是debug日志...");
//SpringBoot默认给我们使用的是info级别的,没有指定级别的就用SpringBoot默认规定的级别;root级别
logger.info("这是info日志...");
logger.warn("这是warn日志...");
logger.error("这是error日志...");


}
日志输出格式:
    %d表示日期时间,
    %thread表示线程名,
    %-5level:级别从左显示5个字符宽度
    %logger{50} 表示logger名字最长50个字符,否则按照句点分割。 
    %msg:日志消息,
    %n是换行符
-->
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n

SpringBoot修改日志的默认配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
logging.level.com.atguigu=trace


#logging.path=
# 不指定路径在当前项目下生成springboot.log日志
# 可以指定完整的路径;
#logging.file=G:/springboot.log

# 在当前磁盘的根路径下创建spring文件夹和里面的log文件夹;使用 spring.log 作为默认文件
logging.path=/spring/log

# 在控制台输出的日志的格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
# 指定文件中日志输出的格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n
logging.file logging.path Example Description
(none) (none) 只在控制台输出
指定文件名 (none) my.log 输出日志到my.log文件
(none) 指定目录 /var/log 输出到指定目录的 spring.log 文件中

2、指定配置

给类路径下放上每个日志框架自己的配置文件即可;SpringBoot就不使用他默认配置的了

Logging System Customization
Logback logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy
Log4j2 log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging) logging.properties

logback.xml:直接就被日志框架识别了;

logback-spring.xml:日志框架就不直接加载日志的配置项,由SpringBoot解析日志配置,可以使用SpringBoot的高级Profile功能

1
2
3
4
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
可以指定某段配置只在某个环境下生效
</springProfile>

如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!--
日志输出格式:
%d表示日期时间,
%thread表示线程名,
%-5level:级别从左显示5个字符宽度
%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
%msg:日志消息,
%n是换行符
-->
<layout class="ch.qos.logback.classic.PatternLayout">
<springProfile name="dev">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] ---> %-5level %logger{50} - %msg%n</pattern>
</springProfile>
<springProfile name="!dev">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ==== [%thread] ==== %-5level %logger{50} - %msg%n</pattern>
</springProfile>
</layout>
</appender>

如果使用logback.xml作为日志配置文件,还要使用profile功能,会有以下错误

no applicable action for [springProfile]

5、切换日志框架

可以按照slf4j的日志适配图,进行相关的切换;

slf4j+log4j的方式;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>

切换为log4j2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

四、Web开发

1、简介

使用SpringBoot;

1)、创建SpringBoot应用,选中我们需要的模块;

2)、SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来

3)、自己编写业务代码;

自动配置原理?

这个场景SpringBoot帮我们配置了什么?能不能修改?能修改哪些配置?能不能扩展?xxx

1
2
xxxxAutoConfiguration:帮我们给容器中自动配置组件;
xxxxProperties:配置类来封装配置文件的内容;

2、SpringBoot对静态资源的映射规则;

1
2
3
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
//可以设置和静态资源有关的参数,缓存时间等
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
64
WebMvcAuotConfiguration:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
//静态资源文件夹映射
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
}

//配置欢迎页映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(
ResourceProperties resourceProperties) {
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}

//配置喜欢的图标
@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration {

private final ResourceProperties resourceProperties;

public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}

@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
//所有 **/favicon.ico
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}

@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler
.setLocations(this.resourceProperties.getFaviconLocations());
return requestHandler;
}

}

==1)、所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;==

​ webjars:以jar包的方式引入静态资源;

http://www.webjars.org/

localhost:8080/webjars/jquery/3.3.1/jquery.js

1
2
3
4
5
6
<!--引入jquery-webjar-->在访问的时候只需要写webjars下面资源的名称即可
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>

==2)、”/**” 访问当前项目的任何资源,都去(静态资源的文件夹)找映射==

1
2
3
4
5
"classpath:/META-INF/resources/", 
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

localhost:8080/abc === 去静态资源文件夹里面找abc

==3)、欢迎页; 静态资源文件夹下的所有index.html页面;被”/**”映射;==

​ localhost:8080/ 找index页面

==4)、所有的 **/favicon.ico 都是在静态资源文件下找;==

3、模板引擎

JSP、Velocity、Freemarker、Thymeleaf

SpringBoot推荐的Thymeleaf;

语法更简单,功能更强大;

1、引入thymeleaf;

1
2
3
4
5
6
7
8
9
10
11
12
		<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
2.1.6
</dependency>
切换thymeleaf版本
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>

2、Thymeleaf使用

1
2
3
4
5
6
7
8
9
10
11
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");

private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

public static final String DEFAULT_PREFIX = "classpath:/templates/";

public static final String DEFAULT_SUFFIX = ".html";
//

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

使用:

1、导入thymeleaf的名称空间

1
<html lang="en" xmlns:th="http://www.thymeleaf.org">

2、使用thymeleaf语法;

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!--th:text 将div里面的文本内容设置为 -->
<div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>

3、语法规则

1)、th:text;改变当前元素里面的文本内容;

​ th:任意html属性;来替换原生属性的值

2)、表达式?

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
64
65
66
67
68
69
Simple expressions:(表达式语法)
Variable Expressions: ${...}:获取变量值;OGNL;
1)、获取对象的属性、调用方法
2)、使用内置的基本对象:
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object.

${session.foo}
3)、内置的一些工具对象:
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).

Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
补充:配合 th:object="${session.user}:
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

Message Expressions: #{...}:获取国际化内容
Link URL Expressions: @{...}:定义URL;
@{/order/process(execId=${execId},execType='FAST')}
Fragment Expressions: ~{...}:片段引用表达式
<div th:insert="~{commons :: main}">...</div>

Literals(字面量)
Text literals: 'one text' , 'Another one!' ,…
Number literals: 0 , 34 , 3.0 , 12.3 ,…
Boolean literals: true , false
Null literal: null
Literal tokens: one , sometext , main ,…
Text operations:(文本操作)
String concatenation: +
Literal substitutions: |The name is ${name}|
Arithmetic operations:(数学运算)
Binary operators: + , - , * , / , %
Minus sign (unary operator): -
Boolean operations:(布尔运算)
Binary operators: and , or
Boolean negation (unary operator): ! , not
Comparisons and equality:(比较运算)
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
Conditional operators:条件运算(三元运算符)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Special tokens:
No-Operation: _

4、SpringMVC自动配置

https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications

1. Spring MVC auto-configuration

Spring Boot 自动配置好了SpringMVC

以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAutoConfiguration)==

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

    • 自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发?重定向?))
    • ContentNegotiatingViewResolver:组合所有的视图解析器的;
    • ==如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来;==
  • Support for serving static resources, including support for WebJars (see below).静态资源文件夹路径,webjars

  • Static index.html support. 静态首页访问

  • Custom Favicon support (see below). favicon.ico

    ​

  • 自动注册了 of Converter, GenericConverter, Formatter beans.

    • Converter:转换器; public String hello(User user):类型转换使用Converter
    • Formatter 格式化器; 2017.12.17===Date;
1
2
3
4
5
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")//在文件中配置日期格式化的规则
public Formatter<Date> dateFormatter() {
return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化组件
}

​ ==自己添加的格式化器转换器,我们只需要放在容器中即可==

  • Support for HttpMessageConverters (see below).

    • HttpMessageConverter:SpringMVC用来转换Http请求和响应的;User—Json;

    • HttpMessageConverters 是从容器中确定;获取所有的HttpMessageConverter;

      ==自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(@Bean,@Component)==

      ​

  • Automatic registration of MessageCodesResolver (see below).定义错误代码生成规则

  • Automatic use of a ConfigurableWebBindingInitializer bean (see below).

    ==我们可以配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器)==

    1
    2
    初始化WebDataBinder;
    请求数据=====JavaBean;

org.springframework.boot.autoconfigure.web:web的所有自动场景;

If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

2、扩展SpringMVC

1
2
3
4
5
6
7
<mvc:view-controller path="/hello" view-name="success"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>

==编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc==;

既保留了所有的自动配置,也能用我们扩展的配置;

1
2
3
4
5
6
7
8
9
10
11
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /atguigu 请求来到 success
registry.addViewController("/atguigu").setViewName("success");
}
}

原理:

​ 1)、WebMvcAutoConfiguration是SpringMVC的自动配置类

​ 2)、在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   @Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

//从容器中获取所有的WebMvcConfigurer
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
//一个参考实现;将所有的WebMvcConfigurer相关配置都来一起调用;
@Override
// public void addViewControllers(ViewControllerRegistry registry) {
// for (WebMvcConfigurer delegate : this.delegates) {
// delegate.addViewControllers(registry);
// }
}
}
}

​ 3)、容器中所有的WebMvcConfigurer都会一起起作用;

​ 4)、我们的配置类也会被调用;

​ 效果:SpringMVC的自动配置和我们的扩展配置都会起作用;

3、全面接管SpringMVC;

SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了

我们需要在配置类中添加@EnableWebMvc即可;

1
2
3
4
5
6
7
8
9
10
11
12
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /atguigu 请求来到 success
registry.addViewController("/atguigu").setViewName("success");
}
}

原理:

为什么@EnableWebMvc自动配置就失效了;

1)@EnableWebMvc的核心

1
2
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {

2)、

1
2
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3)、

1
2
3
4
5
6
7
8
9
10
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
WebMvcConfigurerAdapter.class })
//容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

4)、@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;

5)、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;

5、如何修改SpringBoot的默认配置

模式:

​ 1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;

​ 2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置

​ 3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

6、RestfulCRUD

1)、默认访问首页

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

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc 不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /atguigu 请求来到 success
registry.addViewController("/atguigu").setViewName("success");
}

//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将组件注册在容器
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
}
};
return adapter;
}
}

2)、国际化

1)、编写国际化配置文件;

2)、使用ResourceBundleMessageSource管理国际化资源文件

3)、在页面使用fmt:message取出国际化内容

步骤:

1)、编写国际化配置文件,抽取页面需要显示的国际化消息

2)、SpringBoot自动配置好了管理国际化资源文件的组件;

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
@ConfigurationProperties(prefix = "spring.messages")
public class MessageSourceAutoConfiguration {

/**
* Comma-separated list of basenames (essentially a fully-qualified classpath
* location), each following the ResourceBundle convention with relaxed support for
* slash based locations. If it doesn't contain a package qualifier (such as
* "org.mypackage"), it will be resolved from the classpath root.
*/
private String basename = "messages";
//我们的配置文件可以直接放在类路径下叫messages.properties;

@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(this.basename)) {
//设置国际化资源文件的基础名(去掉语言国家代码的)
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(this.basename)));
}
if (this.encoding != null) {
messageSource.setDefaultEncoding(this.encoding.name());
}
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
messageSource.setCacheSeconds(this.cacheSeconds);
messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
return messageSource;
}

3)、去页面获取国际化的值;

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
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head>

<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<label class="sr-only" th:text="#{login.username}">Username</label>
<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"/> [[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm">中文</a>
<a class="btn btn-sm">English</a>
</form>

</body>

</html>

效果:根据浏览器语言设置的信息切换了国际化;

原理:

​ 国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);

1
2
3
4
5
6
7
8
9
10
11
12
13
		@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() {
if (this.mvcProperties
.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
默认的就是根据请求头带来的区域信息获取Locale进行国际化

4)、点击链接切换国际化

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
/**
* 可以在连接上携带区域信息
*/
public class MyLocaleResolver implements LocaleResolver {

@Override
public Locale resolveLocale(HttpServletRequest request) {
String l = request.getParameter("l");
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(l)){
String[] split = l.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}

@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

}
}


@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}

3)、登陆

开发期间模板引擎页面修改以后,要实时生效

1)、禁用模板引擎的缓存

1
2
# 禁用缓存
spring.thymeleaf.cache=false

2)、页面修改完成以后ctrl+f9:重新编译;

登陆错误消息的显示

1
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

4)、拦截器进行登陆检查

拦截器

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

/**
* 登陆检查,
*/
public class LoginHandlerInterceptor implements HandlerInterceptor {
//目标方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("loginUser");
if(user == null){
//未登陆,返回登陆页面
request.setAttribute("msg","没有权限请先登陆");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else{
//已登陆,放行请求
return true;
}

}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

}
}

注册拦截器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将组件注册在容器
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}

//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
//静态资源; *.css , *.js
//SpringBoot已经做好了静态资源映射
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login");
}
};
return adapter;
}

5)、CRUD-员工列表

实验要求:

1)、RestfulCRUD:CRUD满足Rest风格;

URI: /资源名称/资源标识 HTTP请求方式区分对资源CRUD操作

普通CRUD(uri来区分操作) RestfulCRUD
查询 getEmp emp—GET
添加 addEmp?xxx emp—POST
修改 updateEmp?id=xxx&xxx=xx emp/{id}—PUT
删除 deleteEmp?id=1 emp/{id}—DELETE

2)、实验的请求架构;

实验功能 请求URI 请求方式
查询所有员工 emps GET
查询某个员工(来到修改页面) emp/1 GET
来到添加页面 emp GET
添加员工 emp POST
来到修改页面(查出员工进行信息回显) emp/1 GET
修改员工 emp PUT
删除员工 emp/1 DELETE

3)、员工列表:

thymeleaf公共页面元素抽取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1、抽取公共片段
<div th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</div>

2、引入公共片段
<div th:insert="~{footer :: copy}"></div>
~{templatename::selector}:模板名::选择器
~{templatename::fragmentname}:模板名::片段名

3、默认效果:
insert的公共片段在div标签中
如果使用th:insert等属性进行引入,可以不用写~{}:
行内写法可以加上:[[~{}]];[(~{})];

三种引入公共片段的th属性:

th:insert:将公共片段整个插入到声明引入的元素中

th:replace:将声明引入的元素替换为公共片段

th:include:将被引入的片段的内容包含进这个标签中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<footer th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</footer>

引入方式
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>

效果
<div>
<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>
</div>

<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>

<div>
&copy; 2011 The Good Thymes Virtual Grocery
</div>

引入片段的时候传入参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

<nav class="col-md-2 d-none d-md-block bg-light sidebar" id="sidebar">
<div class="sidebar-sticky">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active"
th:class="${activeUri=='main.html'?'nav-link active':'nav-link'}"
href="#" th:href="@{/main.html}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">
<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
<polyline points="9 22 9 12 15 12 15 22"></polyline>
</svg>
Dashboard <span class="sr-only">(current)</span>
</a>
</li>

<!--引入侧边栏;传入参数-->
<div th:replace="commons/bar::#sidebar(activeUri='emps')"></div>

6)、CRUD-员工添加

添加页面

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
<form>
<div class="form-group">
<label>LastName</label>
<input type="text" class="form-control" placeholder="zhangsan">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" placeholder="zhangsan@atguigu.com">
</div>
<div class="form-group">
<label>Gender</label><br/>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="1">
<label class="form-check-label">男</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="0">
<label class="form-check-label">女</label>
</div>
</div>
<div class="form-group">
<label>department</label>
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="form-group">
<label>Birth</label>
<input type="text" class="form-control" placeholder="zhangsan">
</div>
<button type="submit" class="btn btn-primary">添加</button>
</form>

提交的数据格式不对:生日:日期;

2017-12-12;2017/12/12;2017.12.12;

日期的格式化;SpringMVC将页面提交的值需要转换为指定的类型;

2017-12-12—Date; 类型转换,格式化;

默认日期是按照/的方式;

7)、CRUD-员工修改

修改添加二合一表单

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
<!--需要区分是员工修改还是添加;-->
<form th:action="@{/emp}" method="post">
<!--发送put请求修改员工数据-->
<!--
1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)
2、页面创建一个post表单
3、创建一个input项,name="_method";值就是我们指定的请求方式
-->
<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
<div class="form-group">
<label>LastName</label>
<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">
</div>
<div class="form-group">
<label>Email</label>
<input name="email" type="email" class="form-control" placeholder="zhangsan@atguigu.com" th:value="${emp!=null}?${emp.email}">
</div>
<div class="form-group">
<label>Gender</label><br/>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp!=null}?${emp.gender==1}">
<label class="form-check-label">男</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp!=null}?${emp.gender==0}">
<label class="form-check-label">女</label>
</div>
</div>
<div class="form-group">
<label>department</label>
<!--提交的是部门的id-->
<select class="form-control" name="department.id">
<option th:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option>
</select>
</div>
<div class="form-group">
<label>Birth</label>
<input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">
</div>
<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>
</form>

8)、CRUD-员工删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<tr th:each="emp:${emps}">
<td th:text="${emp.id}"></td>
<td>[[${emp.lastName}]]</td>
<td th:text="${emp.email}"></td>
<td th:text="${emp.gender}==0?'女':'男'"></td>
<td th:text="${emp.department.departmentName}"></td>
<td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>
<td>
<a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.id}">编辑</a>
<button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">删除</button>
</td>
</tr>


<script>
$(".deleteBtn").click(function(){
//删除当前员工的
$("#deleteEmpForm").attr("action",$(this).attr("del_uri")).submit();
return false;
});
</script>

7、错误处理机制

1)、SpringBoot默认的错误处理机制

默认效果:

​ 1)、浏览器,返回一个默认的错误页面

浏览器发送请求的请求头:

​ 2)、如果是其他客户端,默认响应一个json数据

​

原理:

​ 可以参照ErrorMvcAutoConfiguration;错误处理的自动配置;

给容器中添加了以下组件

​ 1、DefaultErrorAttributes:

1
2
3
4
5
6
7
8
9
10
11
帮我们在页面共享信息;
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,
boolean includeStackTrace) {
Map<String, Object> errorAttributes = new LinkedHashMap<String, Object>();
errorAttributes.put("timestamp", new Date());
addStatus(errorAttributes, requestAttributes);
addErrorDetails(errorAttributes, requestAttributes, includeStackTrace);
addPath(errorAttributes, requestAttributes);
return errorAttributes;
}

​ 2、BasicErrorController:处理默认/error请求

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
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {

@RequestMapping(produces = "text/html")//产生html类型的数据;浏览器发送的请求来到这个方法处理
public ModelAndView errorHtml(HttpServletRequest request,
HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());

//去哪个页面作为错误页面;包含页面地址和页面内容
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
return (modelAndView == null ? new ModelAndView("error", model) : modelAndView);
}

@RequestMapping
@ResponseBody //产生json数据,其他客户端来到这个方法处理;
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}

​ 3、ErrorPageCustomizer:

1
2
@Value("${error.path:/error}")
private String path = "/error"; 系统出现错误以后来到error请求进行处理;(web.xml注册的错误页面规则)

​ 4、DefaultErrorViewResolver:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status,
Map<String, Object> model) {
ModelAndView modelAndView = resolve(String.valueOf(status), model);
if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) {
modelAndView = resolve(SERIES_VIEWS.get(status.series()), model);
}
return modelAndView;
}

private ModelAndView resolve(String viewName, Map<String, Object> model) {
//默认SpringBoot可以去找到一个页面? error/404
String errorViewName = "error/" + viewName;

//模板引擎可以解析这个页面地址就用模板引擎解析
TemplateAvailabilityProvider provider = this.templateAvailabilityProviders
.getProvider(errorViewName, this.applicationContext);
if (provider != null) {
//模板引擎可用的情况下返回到errorViewName指定的视图地址
return new ModelAndView(errorViewName, model);
}
//模板引擎不可用,就在静态资源文件夹下找errorViewName对应的页面 error/404.html
return resolveResource(errorViewName, model);
}

​ 步骤:

​ 一但系统出现4xx或者5xx之类的错误;ErrorPageCustomizer就会生效(定制错误的响应规则);就会来到/error请求;就会被BasicErrorController处理;

​ 1)响应页面;去哪个页面是由DefaultErrorViewResolver解析得到的;

1
2
3
4
5
6
7
8
9
10
11
protected ModelAndView resolveErrorView(HttpServletRequest request,
HttpServletResponse response, HttpStatus status, Map<String, Object> model) {
//所有的ErrorViewResolver得到ModelAndView
for (ErrorViewResolver resolver : this.errorViewResolvers) {
ModelAndView modelAndView = resolver.resolveErrorView(request, status, model);
if (modelAndView != null) {
return modelAndView;
}
}
return null;
}

2)、如果定制错误响应:

1)、如何定制错误的页面;

​ 1)、有模板引擎的情况下;error/状态码; 【将错误页面命名为 错误状态码.html 放在模板引擎文件夹里面的 error文件夹下】,发生此状态码的错误就会来到 对应的页面;

​ 我们可以使用4xx和5xx作为错误页面的文件名来匹配这种类型的所有错误,精确优先(优先寻找精确的状态码.html);

​ 页面能获取的信息;

​ timestamp:时间戳

​ status:状态码

​ error:错误提示

​ exception:异常对象

​ message:异常消息

​ errors:JSR303数据校验的错误都在这里

​ 2)、没有模板引擎(模板引擎找不到这个错误页面),静态资源文件夹下找;

​ 3)、以上都没有错误页面,就是默认来到SpringBoot默认的错误提示页面;

2)、如何定制错误的json数据;

​ 1)、自定义异常处理&返回定制json数据;

1
2
3
4
5
6
7
8
9
10
11
12
13
@ControllerAdvice
public class MyExceptionHandler {

@ResponseBody
@ExceptionHandler(UserNotExistException.class)
public Map<String,Object> handleException(Exception e){
Map<String,Object> map = new HashMap<>();
map.put("code","user.notexist");
map.put("message",e.getMessage());
return map;
}
}
//没有自适应效果...

​ 2)、转发到/error进行自适应响应效果处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@ExceptionHandler(UserNotExistException.class)
public String handleException(Exception e, HttpServletRequest request){
Map<String,Object> map = new HashMap<>();
//传入我们自己的错误状态码 4xx 5xx,否则就不会进入定制错误页面的解析流程
/**
* Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
*/
request.setAttribute("javax.servlet.error.status_code",500);
map.put("code","user.notexist");
map.put("message",e.getMessage());
//转发到/error
return "forward:/error";
}

3)、将我们的定制数据携带出去;

出现错误以后,会来到/error请求,会被BasicErrorController处理,响应出去可以获取的数据是由getErrorAttributes得到的(是AbstractErrorController(ErrorController)规定的方法);

​ 1、完全来编写一个ErrorController的实现类【或者是编写AbstractErrorController的子类】,放在容器中;

​ 2、页面上能用的数据,或者是json返回能用的数据都是通过errorAttributes.getErrorAttributes得到;

​ 容器中DefaultErrorAttributes.getErrorAttributes();默认进行数据处理的;

自定义ErrorAttributes

1
2
3
4
5
6
7
8
9
10
11
//给容器中加入我们自己定义的ErrorAttributes
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {

@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> map = super.getErrorAttributes(requestAttributes, includeStackTrace);
map.put("company","atguigu");
return map;
}
}

最终的效果:响应是自适应的,可以通过定制ErrorAttributes改变需要返回的内容,

8、配置嵌入式Servlet容器

SpringBoot默认使用Tomcat作为嵌入式的Servlet容器;

问题?

1)、如何定制和修改Servlet容器的相关配置;

1、修改和server有关的配置(ServerProperties【也是EmbeddedServletContainerCustomizer】);

1
2
3
4
5
6
7
8
9
server.port=8081
server.context-path=/crud

server.tomcat.uri-encoding=UTF-8

//通用的Servlet容器设置
server.xxx
//Tomcat的设置
server.tomcat.xxx

2、编写一个EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定制器;来修改Servlet容器的配置

1
2
3
4
5
6
7
8
9
10
11
@Bean  //一定要将这个定制器加入到容器中
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
return new EmbeddedServletContainerCustomizer() {

//定制嵌入式的Servlet容器相关的规则
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8083);
}
};
}

2)、注册Servlet三大组件【Servlet、Filter、Listener】

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。

注册三大组件用以下方式

ServletRegistrationBean

1
2
3
4
5
6
//注册三大组件
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return registrationBean;
}

FilterRegistrationBean

1
2
3
4
5
6
7
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}

ServletListenerRegistrationBean

1
2
3
4
5
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return registrationBean;
}

SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;DIspatcherServlet;

DispatcherServletAutoConfiguration中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public ServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet, this.serverProperties.getServletMapping());
//默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求; /*会拦截jsp
//可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径

registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}

2)、SpringBoot能不能支持其他的Servlet容器;

3)、替换为其他嵌入式Servlet容器

默认支持:

Tomcat(默认使用)

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
引入web模块默认就是使用嵌入式的Tomcat作为Servlet容器;
</dependency>

Jetty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- 引入web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>

<!--引入其他的Servlet容器-->
<dependency>
<artifactId>spring-boot-starter-jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>

Undertow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- 引入web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>

<!--引入其他的Servlet容器-->
<dependency>
<artifactId>spring-boot-starter-undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>

4)、嵌入式Servlet容器自动配置原理;

EmbeddedServletContainerAutoConfiguration:嵌入式的Servlet容器自动配置?

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
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@Import(BeanPostProcessorsRegistrar.class)
//导入BeanPostProcessorsRegistrar:Spring注解版;给容器中导入一些组件
//导入了EmbeddedServletContainerCustomizerBeanPostProcessor:
//后置处理器:bean初始化前后(创建完对象,还没赋值赋值)执行初始化工作
public class EmbeddedServletContainerAutoConfiguration {

@Configuration
@ConditionalOnClass({ Servlet.class, Tomcat.class })//判断当前是否引入了Tomcat依赖;
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)//判断当前容器没有用户自己定义EmbeddedServletContainerFactory:嵌入式的Servlet容器工厂;作用:创建嵌入式的Servlet容器
public static class EmbeddedTomcat {

@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
return new TomcatEmbeddedServletContainerFactory();
}

}

/**
* Nested configuration if Jetty is being used.
*/
@Configuration
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class,
WebAppContext.class })
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedJetty {

@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
return new JettyEmbeddedServletContainerFactory();
}

}

/**
* Nested configuration if Undertow is being used.
*/
@Configuration
@ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedUndertow {

@Bean
public UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory() {
return new UndertowEmbeddedServletContainerFactory();
}

}

1)、EmbeddedServletContainerFactory(嵌入式Servlet容器工厂)

1
2
3
4
5
6
7
public interface EmbeddedServletContainerFactory {

//获取嵌入式的Servlet容器
EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers);

}

2)、EmbeddedServletContainer:(嵌入式的Servlet容器)

3)、以TomcatEmbeddedServletContainerFactory为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers) {
//创建一个Tomcat
Tomcat tomcat = new Tomcat();

//配置Tomcat的基本环节
File baseDir = (this.baseDirectory != null ? this.baseDirectory
: createTempDir("tomcat"));
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);

//将配置好的Tomcat传入进去,返回一个EmbeddedServletContainer;并且启动Tomcat服务器
return getTomcatEmbeddedServletContainer(tomcat);
}

4)、我们对嵌入式容器的配置修改是怎么生效?

1
ServerProperties、EmbeddedServletContainerCustomizer

EmbeddedServletContainerCustomizer:定制器帮我们修改了Servlet容器的配置?

怎么修改的原理?

5)、容器中导入了EmbeddedServletContainerCustomizerBeanPostProcessor

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
//初始化之前
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
//如果当前初始化的是一个ConfigurableEmbeddedServletContainer类型的组件
if (bean instanceof ConfigurableEmbeddedServletContainer) {
//
postProcessBeforeInitialization((ConfigurableEmbeddedServletContainer) bean);
}
return bean;
}

private void postProcessBeforeInitialization(
ConfigurableEmbeddedServletContainer bean) {
//获取所有的定制器,调用每一个定制器的customize方法来给Servlet容器进行属性赋值;
for (EmbeddedServletContainerCustomizer customizer : getCustomizers()) {
customizer.customize(bean);
}
}

private Collection<EmbeddedServletContainerCustomizer> getCustomizers() {
if (this.customizers == null) {
// Look up does not include the parent context
this.customizers = new ArrayList<EmbeddedServletContainerCustomizer>(
this.beanFactory
//从容器中获取所有这葛类型的组件:EmbeddedServletContainerCustomizer
//定制Servlet容器,给容器中可以添加一个EmbeddedServletContainerCustomizer类型的组件
.getBeansOfType(EmbeddedServletContainerCustomizer.class,
false, false)
.values());
Collections.sort(this.customizers, AnnotationAwareOrderComparator.INSTANCE);
this.customizers = Collections.unmodifiableList(this.customizers);
}
return this.customizers;
}

ServerProperties也是定制器

步骤:

1)、SpringBoot根据导入的依赖情况,给容器中添加相应的EmbeddedServletContainerFactory【TomcatEmbeddedServletContainerFactory】

2)、容器中某个组件要创建对象就会惊动后置处理器;EmbeddedServletContainerCustomizerBeanPostProcessor;

只要是嵌入式的Servlet容器工厂,后置处理器就工作;

3)、后置处理器,从容器中获取所有的EmbeddedServletContainerCustomizer,调用定制器的定制方法

###5)、嵌入式Servlet容器启动原理;

什么时候创建嵌入式的Servlet容器工厂?什么时候获取嵌入式的Servlet容器并启动Tomcat;

获取嵌入式的Servlet容器工厂:

1)、SpringBoot应用启动运行run方法

2)、refreshContext(context);SpringBoot刷新IOC容器【创建IOC容器对象,并初始化容器,创建容器中的每一个组件】;如果是web应用创建AnnotationConfigEmbeddedWebApplicationContext,否则:AnnotationConfigApplicationContext

3)、refresh(context);刷新刚才创建好的ioc容器;

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
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);

try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.
initMessageSource();

// Initialize event multicaster for this context.
initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
onRefresh();

// Check for listener beans and register them.
registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
finishRefresh();
}

catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}

// Destroy already created singletons to avoid dangling resources.
destroyBeans();

// Reset 'active' flag.
cancelRefresh(ex);

// Propagate exception to caller.
throw ex;
}

finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}

4)、 onRefresh(); web的ioc容器重写了onRefresh方法

5)、webioc容器会创建嵌入式的Servlet容器;createEmbeddedServletContainer();

6)、获取嵌入式的Servlet容器工厂:

EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();

​ 从ioc容器中获取EmbeddedServletContainerFactory 组件;TomcatEmbeddedServletContainerFactory创建对象,后置处理器一看是这个对象,就获取所有的定制器来先定制Servlet容器的相关配置;

7)、使用容器工厂获取嵌入式的Servlet容器:this.embeddedServletContainer = containerFactory .getEmbeddedServletContainer(getSelfInitializer());

8)、嵌入式的Servlet容器创建对象并启动Servlet容器;

先启动嵌入式的Servlet容器,再将ioc容器中剩下没有创建出的对象获取出来;

==IOC容器启动创建嵌入式的Servlet容器==

9、使用外置的Servlet容器

嵌入式Servlet容器:应用打成可执行的jar

​ 优点:简单、便携;

​ 缺点:默认不支持JSP、优化定制比较复杂(使用定制器【ServerProperties、自定义EmbeddedServletContainerCustomizer】,自己编写嵌入式Servlet容器的创建工厂【EmbeddedServletContainerFactory】);

外置的Servlet容器:外面安装Tomcat—应用war包的方式打包;

步骤

1)、必须创建一个war项目;(利用idea创建好目录结构)

2)、将嵌入式的Tomcat指定为provided;

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

3)、必须编写一个SpringBootServletInitializer的子类,并调用configure方法

1
2
3
4
5
6
7
8
9
public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//传入SpringBoot应用的主程序
return application.sources(SpringBoot04WebJspApplication.class);
}

}

4)、启动服务器就可以使用;

原理

jar包:执行SpringBoot主类的main方法,启动ioc容器,创建嵌入式的Servlet容器;

war包:启动服务器,服务器启动SpringBoot应用【SpringBootServletInitializer】,启动ioc容器;

servlet3.0(Spring注解版):

8.2.4 Shared libraries / runtimes pluggability:

规则:

​ 1)、服务器启动(web应用启动)会创建当前web应用里面每一个jar包里面ServletContainerInitializer实例:

​ 2)、ServletContainerInitializer的实现放在jar包的META-INF/services文件夹下,有一个名为javax.servlet.ServletContainerInitializer的文件,内容就是ServletContainerInitializer的实现类的全类名

​ 3)、还可以使用@HandlesTypes,在应用启动的时候加载我们感兴趣的类;

流程:

1)、启动Tomcat

2)、org\springframework\spring-web\4.3.14.RELEASE\spring-web-4.3.14.RELEASE.jar!\META-INF\services\javax.servlet.ServletContainerInitializer:

Spring的web模块里面有这个文件:org.springframework.web.SpringServletContainerInitializer

3)、SpringServletContainerInitializer将@HandlesTypes(WebApplicationInitializer.class)标注的所有这个类型的类都传入到onStartup方法的Set<Class<?>>;为这些WebApplicationInitializer类型的类创建实例;

4)、每一个WebApplicationInitializer都调用自己的onStartup;

5)、相当于我们的SpringBootServletInitializer的类会被创建对象,并执行onStartup方法

6)、SpringBootServletInitializer实例执行onStartup的时候会createRootApplicationContext;创建容器

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
protected WebApplicationContext createRootApplicationContext(
ServletContext servletContext) {
//1、创建SpringApplicationBuilder
SpringApplicationBuilder builder = createSpringApplicationBuilder();
StandardServletEnvironment environment = new StandardServletEnvironment();
environment.initPropertySources(servletContext, null);
builder.environment(environment);
builder.main(getClass());
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
if (parent != null) {
this.logger.info("Root context already created (using as parent).");
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
builder.initializers(new ParentContextApplicationContextInitializer(parent));
}
builder.initializers(
new ServletContextApplicationContextInitializer(servletContext));
builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);

//调用configure方法,子类重写了这个方法,将SpringBoot的主程序类传入了进来
builder = configure(builder);

//使用builder创建一个Spring应用
SpringApplication application = builder.build();
if (application.getSources().isEmpty() && AnnotationUtils
.findAnnotation(getClass(), Configuration.class) != null) {
application.getSources().add(getClass());
}
Assert.state(!application.getSources().isEmpty(),
"No SpringApplication sources have been defined. Either override the "
+ "configure method or add an @Configuration annotation");
// Ensure error pages are registered
if (this.registerErrorPageFilter) {
application.getSources().add(ErrorPageFilterConfiguration.class);
}
//启动Spring应用
return run(application);
}

7)、Spring的应用就启动并且创建IOC容器

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
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
analyzers = new FailureAnalyzers(context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);

//刷新IOC容器
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
}

==启动Servlet容器,再启动SpringBoot应用==

五、Docker

1、简介

Docker是一个开源的应用容器引擎;是一个轻量级容器技术;

Docker支持将软件编译成一个镜像;然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使用这个镜像;

运行中的这个镜像称为容器,容器启动是非常快速的。

2、核心概念

docker主机(Host):安装了Docker程序的机器(Docker直接安装在操作系统之上);

docker客户端(Client):连接docker主机进行操作;

docker仓库(Registry):用来保存各种打包好的软件镜像;

docker镜像(Images):软件打包好的镜像;放在docker仓库中;

docker容器(Container):镜像启动后的实例称为一个容器;容器是独立运行的一个或一组应用

使用Docker的步骤:

1)、安装Docker

2)、去Docker仓库找到这个软件对应的镜像;

3)、使用Docker运行这个镜像,这个镜像就会生成一个Docker容器;

4)、对容器的启动停止就是对软件的启动停止;

3、安装Docker

1)、安装linux虚拟机

​ 1)、VMWare、VirtualBox(安装);

​ 2)、导入虚拟机文件centos7-atguigu.ova;

​ 3)、双击启动linux虚拟机;使用 root/ 123456登陆

​ 4)、使用客户端连接linux服务器进行命令操作;

​ 5)、设置虚拟机网络;

​ 桥接网络===选好网卡====接入网线;

​ 6)、设置好网络以后使用命令重启虚拟机的网络

1
service network restart

​ 7)、查看linux的ip地址

1
ip addr

​ 8)、使用客户端连接linux;

2)、在linux虚拟机上安装docker

步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1、检查内核版本,必须是3.10及以上
uname -r
2、安装docker
yum install docker
3、输入y确认安装
4、启动docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker -v
Docker version 1.12.6, build 3e8e77d/1.12.6
5、开机启动docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
6、停止docker
systemctl stop docker

4、Docker常用命令&操作

1)、镜像操作

操作 命令 说明
检索 docker search 关键字 eg:docker search redis 我们经常去docker hub上检索镜像的详细信息,如镜像的TAG。
拉取 docker pull 镜像名:tag :tag是可选的,tag表示标签,多为软件的版本,默认是latest
列表 docker images 查看所有本地镜像
删除 docker rmi image-id 删除指定的本地镜像

https://hub.docker.com/

2)、容器操作

软件镜像(QQ安装程序)—-运行镜像—-产生一个容器(正在运行的软件,运行的QQ);

步骤:

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
1、搜索镜像
[root@localhost ~]# docker search tomcat
2、拉取镜像
[root@localhost ~]# docker pull tomcat
3、根据镜像启动容器
docker run --name mytomcat -d tomcat:latest
4、docker ps
查看运行中的容器
5、 停止运行中的容器
docker stop 容器的id
6、查看所有的容器
docker ps -a
7、启动容器
docker start 容器id
8、删除一个容器
docker rm 容器id
9、启动一个做了端口映射的tomcat
[root@localhost ~]# docker run -d -p 8888:8080 tomcat
-d:后台运行
-p: 将主机的端口映射到容器的一个端口 主机端口:容器内部的端口

10、为了演示简单关闭了linux的防火墙
service firewalld status ;查看防火墙状态
service firewalld stop:关闭防火墙
11、查看容器的日志
docker logs container-name/container-id

更多命令参看
https://docs.docker.com/engine/reference/commandline/docker/
可以参考每一个镜像的文档

3)、安装MySQL示例(如下示例命令使用5.x版本,8.x版本参考最新dochub文档)

1
2
3
4
docker pull mysql

docker pull mysql:5.5
docker run -p 3306:3306 --name mysql01 -e MYSQL_ROOT_PASSWORD=12345 -d mysql:5.5

错误的启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost ~]# docker run --name mysql01 -d mysql
42f09819908bb72dd99ae19e792e0a5d03c48638421fa64cce5f8ba0f40f5846

mysql退出了
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42f09819908b mysql "docker-entrypoint.sh" 34 seconds ago Exited (1) 33 seconds ago mysql01
538bde63e500 tomcat "catalina.sh run" About an hour ago Exited (143) About an hour ago compassionate_
goldstine
c4f1ac60b3fc tomcat "catalina.sh run" About an hour ago Exited (143) About an hour ago lonely_fermi
81ec743a5271 tomcat "catalina.sh run" About an hour ago Exited (143) About an hour ago sick_ramanujan


//错误日志
[root@localhost ~]# docker logs 42f09819908b
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD;这个三个参数必须指定一个

正确的启动

1
2
3
4
5
[root@localhost ~]# docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
b874c56bec49fb43024b3805ab51e9097da779f2f572c22c695305dedd684c5f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b874c56bec49 mysql "docker-entrypoint.sh" 4 seconds ago Up 3 seconds 3306/tcp mysql01

做了端口映射

1
2
3
4
5
[root@localhost ~]# docker run -p 3306:3306 --name mysql02 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
ad10e4bc5c6a0f61cbad43898de71d366117d120e39db651844c0e73863b9434
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad10e4bc5c6a mysql "docker-entrypoint.sh" 4 seconds ago Up 2 seconds 0.0.0.0:3306->3306/tcp mysql02

几个其他的高级操作

1
2
3
4
5
6
docker run --name mysql03 -v /conf/mysql:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
把主机的/conf/mysql文件夹挂载到 mysqldocker容器的/etc/mysql/conf.d文件夹里面
改mysql的配置文件就只需要把mysql配置文件放在自定义的文件夹下(/conf/mysql)

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
指定mysql的一些配置参数

六、SpringBoot与数据访问

1、JDBC

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
1
2
3
4
5
6
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.15.22:3306/jdbc
driver-class-name: com.mysql.jdbc.Driver

效果:

​ 默认是用org.apache.tomcat.jdbc.pool.DataSource作为数据源;

​ 数据源的相关配置都在DataSourceProperties里面;

自动配置原理:

org.springframework.boot.autoconfigure.jdbc:

1、参考DataSourceConfiguration,根据配置创建数据源,默认使用Tomcat连接池;可以使用spring.datasource.type指定自定义的数据源类型;

2、SpringBoot默认可以支持;

1
org.apache.tomcat.jdbc.pool.DataSource、HikariDataSource、BasicDataSource、

3、自定义数据源类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* Generic DataSource configuration.
*/
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type")
static class Generic {

@Bean
public DataSource dataSource(DataSourceProperties properties) {
//使用DataSourceBuilder创建数据源,利用反射创建响应type的数据源,并且绑定相关属性
return properties.initializeDataSourceBuilder().build();
}

}

4、DataSourceInitializer:ApplicationListener;

​ 作用:

​ 1)、runSchemaScripts();运行建表语句;

​ 2)、runDataScripts();运行插入数据的sql语句;

默认只需要将文件命名为:

1
2
3
4
5
6
schema-*.sql、data-*.sql
默认规则:schema.sql,schema-all.sql;
可以使用
schema:
- classpath:department.sql
指定位置

5、操作数据库:自动配置了JdbcTemplate操作数据库

2、整合Druid数据源

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
导入druid数据源
@Configuration
public class DruidConfig {

@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}

//配置Druid的监控
//1、配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();

initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");//默认就是允许所有访问
initParams.put("deny","192.168.15.21");

bean.setInitParameters(initParams);
return bean;
}


//2、配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());

Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");

bean.setInitParameters(initParams);

bean.setUrlPatterns(Arrays.asList("/*"));

return bean;
}
}

3、整合MyBatis

1
2
3
4
5
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>

步骤:

​ 1)、配置数据源相关属性(见上一节Druid)

​ 2)、给数据库建表

​ 3)、创建JavaBean

4)、注解版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//指定这是一个操作数据库的mapper
@Mapper
public interface DepartmentMapper {

@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);

@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);

@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(departmentName) values(#{departmentName})")
public int insertDept(Department department);

@Update("update department set departmentName=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}

问题:

自定义MyBatis的配置规则;给容器中添加一个ConfigurationCustomizer;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {

@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){

@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
1
2
3
4
5
6
7
8
9
使用MapperScan批量扫描所有的Mapper接口;
@MapperScan(value = "com.atguigu.springboot.mapper")
@SpringBootApplication
public class SpringBoot06DataMybatisApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBoot06DataMybatisApplication.class, args);
}
}

5)、配置文件版

1
2
3
mybatis:
config-location: classpath:mybatis/mybatis-config.xml 指定全局配置文件的位置
mapper-locations: classpath:mybatis/mapper/*.xml 指定sql映射文件的位置

更多使用参照

http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

4、整合SpringData JPA

1)、SpringData简介

2)、整合SpringData JPA

JPA:ORM(Object Relational Mapping);

1)、编写一个实体类(bean)和数据表进行映射,并且配置好映射关系;

1
2
3
4
5
6
7
8
9
10
11
12
13
//使用JPA注解配置映射关系
@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "tbl_user") //@Table来指定和哪个数据表对应;如果省略默认表名就是user;
public class User {

@Id //这是一个主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键
private Integer id;

@Column(name = "last_name",length = 50) //这是和数据表对应的一个列
private String lastName;
@Column //省略默认列名就是属性名
private String email;

2)、编写一个Dao接口来操作实体类对应的数据表(Repository)

1
2
3
//继承JpaRepository来完成对数据库的操作
public interface UserRepository extends JpaRepository<User,Integer> {
}

3)、基本的配置JpaProperties

1
2
3
4
5
6
7
spring:  
jpa:
hibernate:
# 更新或者创建数据表结构
ddl-auto: update
# 控制台显示SQL
show-sql: true

七、启动配置原理

几个重要的事件回调机制

配置在META-INF/spring.factories

ApplicationContextInitializer

SpringApplicationRunListener

只需要放在ioc容器中

ApplicationRunner

CommandLineRunner

启动流程:

1、创建SpringApplication对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
initialize(sources);
private void initialize(Object[] sources) {
//保存主配置类
if (sources != null && sources.length > 0) {
this.sources.addAll(Arrays.asList(sources));
}
//判断当前是否一个web应用
this.webEnvironment = deduceWebEnvironment();
//从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
//从类路径下找到ETA-INF/spring.factories配置的所有ApplicationListener
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//从多个配置类中找到有main方法的主配置类
this.mainApplicationClass = deduceMainApplicationClass();
}

2、运行run方法

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
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();

//获取SpringApplicationRunListeners;从类路径下META-INF/spring.factories
SpringApplicationRunListeners listeners = getRunListeners(args);
//回调所有的获取SpringApplicationRunListener.starting()方法
listeners.starting();
try {
//封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//准备环境
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
//创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成

Banner printedBanner = printBanner(environment);

//创建ApplicationContext;决定创建web的ioc还是普通的ioc
context = createApplicationContext();

analyzers = new FailureAnalyzers(context);
//准备上下文环境;将environment保存到ioc中;而且applyInitializers();
//applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法
//回调所有的SpringApplicationRunListener的contextPrepared();
//
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
//prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded();

//s刷新容器;ioc容器初始化(如果是web应用还会创建嵌入式的Tomcat);Spring注解版
//扫描,创建,加载所有组件的地方;(配置类,组件,自动配置)
refreshContext(context);
//从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
//ApplicationRunner先回调,CommandLineRunner再回调
afterRefresh(context, applicationArguments);
//所有的SpringApplicationRunListener回调finished方法
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
//整个SpringBoot应用启动完成以后返回启动的ioc容器;
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
}

3、事件监听机制

配置在META-INF/spring.factories

ApplicationContextInitializer

1
2
3
4
5
6
public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("ApplicationContextInitializer...initialize..."+applicationContext);
}
}

SpringApplicationRunListener

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
public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {

//必须有的构造器
public HelloSpringApplicationRunListener(SpringApplication application, String[] args){

}

@Override
public void starting() {
System.out.println("SpringApplicationRunListener...starting...");
}

@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
Object o = environment.getSystemProperties().get("os.name");
System.out.println("SpringApplicationRunListener...environmentPrepared.."+o);
}

@Override
public void contextPrepared(ConfigurableApplicationContext context) {
System.out.println("SpringApplicationRunListener...contextPrepared...");
}

@Override
public void contextLoaded(ConfigurableApplicationContext context) {
System.out.println("SpringApplicationRunListener...contextLoaded...");
}

@Override
public void finished(ConfigurableApplicationContext context, Throwable exception) {
System.out.println("SpringApplicationRunListener...finished...");
}
}

配置(META-INF/spring.factories)

1
2
3
4
5
org.springframework.context.ApplicationContextInitializer=\
com.atguigu.springboot.listener.HelloApplicationContextInitializer

org.springframework.boot.SpringApplicationRunListener=\
com.atguigu.springboot.listener.HelloSpringApplicationRunListener

只需要放在ioc容器中

ApplicationRunner

1
2
3
4
5
6
7
@Component
public class HelloApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner...run....");
}
}

CommandLineRunner

1
2
3
4
5
6
7
@Component
public class HelloCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner...run..."+ Arrays.asList(args));
}
}

八、自定义starter

starter:

​ 1、这个场景需要使用到的依赖是什么?

​ 2、如何编写自动配置

1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration  //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件

@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中

自动配置类要能加载
将需要启动就加载的自动配置类,配置在META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

​ 3、模式:

启动器只用来做依赖导入;

专门来写一个自动配置模块;

启动器依赖自动配置;别人只需要引入启动器(starter)

mybatis-spring-boot-starter;自定义启动器名-spring-boot-starter

步骤:

1)、启动器模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>

<!--启动器-->
<dependencies>

<!--引入自动配置模块-->
<dependency>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

</project>

2)、自动配置模块

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
<?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.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>atguigu-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>

<!--引入spring-boot-starter;所有starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

</dependencies>



</project>
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
package com.atguigu.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "atguigu.hello")
public class HelloProperties {

private String prefix;
private String suffix;

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getSuffix() {
return suffix;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.atguigu.starter;

public class HelloService {

HelloProperties helloProperties;

public HelloProperties getHelloProperties() {
return helloProperties;
}

public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}

public String sayHellAtguigu(String name){
return helloProperties.getPrefix()+"-" +name + helloProperties.getSuffix();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.atguigu.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}

更多SpringBoot整合示例

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

summer-2019

发表于 2019-07-12 更新于 2019-09-12 分类于 其他

Day 1

  1. 海洋馆+动物园 携程购票
  2. 国家图书馆
  3. 鸟巢+水立方

Day 2

  1. 自然博物馆
    • 周一闭馆 提前1-3天预约
    • 预约方式(三种): 电话010-67027702 / 公众号”北京自然博物馆” / 网站 http://www.bmnh.org.cn/ 注:暑期人多,公众号和网站晚上12:00比较好约
    • 预约参观时间段:上午9:00—12:00,下午13:00—16:00
    • 紧邻天坛公园,看时间安排
  2. 古动物馆
    • 周一闭馆 不用预约,当天直接购票参观
    • 参观时间9:00-16:30 咨询电话:010-88369280 010-88369210 010-88369315
    • 紧邻天文馆,看时间安排(提前1-7天预约)
  3. 军事博物馆
    • 周一闭馆 提前1-10天预约(010-66866244) 参观时间:09:00至17:00

Day 3

  1. 颐和园
  2. 圆明园
  3. 清华、北大
    • 北大: https://visitor.pku.edu.cn/visitor/modules/tourist/indexW.html#/touristWeb
    • 清华: 个人参观可提前7天通过“参观清华”微信小程序进行预约

Day 4

  1. 天安门升国旗:看时间安排
  2. 故宫
    • 周一闭馆 提前1-10天预约(https://gugong.ktmtech.cn/)
  3. 国家博物馆
    • 周一闭馆 提前1-5天预约 (http://ticket.chnmuseum.cn/yuyue/index)
    • 入馆时段: 09:00-11:30 12:30-14:00 14:00-16:00
  4. 毛主席纪念堂
    • 周一闭馆,看当天时间安排

Day 5

  1. 八达岭长城
  2. 八达岭野生动物园

Day 6

  1. 世界花卉大观园

daning

发表于 2019-07-10 更新于 2019-09-12 分类于 其他
Welcome to my blog, enter password to read.
阅读全文 »

springboot2-integration-prometheus

发表于 2019-07-09 更新于 2019-09-12 分类于 springboot

springboot2集成prometheus并配置grafana图表

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

application.properties

1
2
3
management.endpoints.web.exposure.include=*
spring.application.name=springboot_prometheus2
management.metrics.tags.application=${spring.application.name}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import io.micrometer.core.instrument.MeterRegistry;

@SpringBootApplication
public class Springboot2PrometheusApplication {

public static void main(String[] args) {
SpringApplication.run(Springboot2PrometheusApplication.class, args);
}

@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}

springboot2启用/actuator/prometheus端点,供Prometheus来抓取指标。

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
/actuator/prometheus

# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds{application="springboot_prometheus",} 1528.62
# HELP jvm_gc_memory_allocated_bytes_total Incremented for an increase in the size of the young generation memory pool after one GC to before the next
# TYPE jvm_gc_memory_allocated_bytes_total counter
jvm_gc_memory_allocated_bytes_total{application="springboot_prometheus",} 1.27722912E8
# HELP system_cpu_count The number of processors available to the Java virtual machine
# TYPE system_cpu_count gauge
system_cpu_count{application="springboot_prometheus",} 8.0
# HELP tomcat_global_error_total
# TYPE tomcat_global_error_total counter
tomcat_global_error_total{application="springboot_prometheus",name="http-nio-8081",} 2.0
# HELP tomcat_threads_busy_threads
# TYPE tomcat_threads_busy_threads gauge
tomcat_threads_busy_threads{application="springboot_prometheus",name="http-nio-8081",} 1.0
# HELP tomcat_threads_current_threads
# TYPE tomcat_threads_current_threads gauge
tomcat_threads_current_threads{application="springboot_prometheus",name="http-nio-8081",} 10.0
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
# TYPE jvm_memory_max_bytes gauge
jvm_memory_max_bytes{application="springboot_prometheus",area="heap",id="PS Survivor Space",} 1.048576E7
jvm_memory_max_bytes{application="springboot_prometheus",area="heap",id="PS Old Gen",} 2.776629248E9
jvm_memory_max_bytes{application="springboot_prometheus",area="nonheap",id="Code Cache",} 2.5165824E8
jvm_memory_max_bytes{application="springboot_prometheus",area="heap",id="PS Eden Space",} 1.367867392E9
jvm_memory_max_bytes{application="springboot_prometheus",area="nonheap",id="Compressed Class Space",} 1.073741824E9
jvm_memory_max_bytes{application="springboot_prometheus",area="nonheap",id="Metaspace",} -1.0

配置grafana曲线

grafana模板:https://grafana.com/dashboards/4701
在Grafana内点击如图所示import按钮,在如图所示位置填写4701,然后点击load。

源码

https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_prometheus
https://github.com/vacual/spring-boot-examples/tree/master/spring-boot-micrometer-vacual

启动程序

1
2
3
ps -ef|grep java | grep -v grep 
root 1133 1 0 May24 ? 01:12:30 java -jar spring-boot-prometheus-1.0.jar
root 8847 1 1 14:20 ? 00:00:29 java -jar -Dserver.port=8081 spring-boot-micrometer1-2.0.0.jar

prometheus配置

1
2
3
4
5
6

- job_name: 'springboot_prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['127.0.0.1:8081']

参考

聊聊springboot2的micrometer
SpringBoot使用prometheus监控

Hello World

发表于 2018-06-10 更新于 2019-07-12

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

v

v

9 日志
6 分类
10 标签
GitHub E-Mail
© 2019 v
由 Hexo 强力驱动 v3.9.0
|
主题 – NexT.Pisces v7.2.0
|