
1.8 使用Spring Tool Suite 4
Spring Tool Suite可理解为基于Eclipse等工具的一个套件,提供了一个已封装、可使用环境来快速生成、实现、调试、运行和部署Spring Boot应用,特别适合于想快速理解和使用Spring Boot 2的开发人员。目前最新版本是4.x,支持生成基本Spring Boot 2项目框架。
有多种获得和安装Spring Tool Suite 4的方法,一种是直接从官网下载已经集成了Spring Tool Suite 4的Eclipse,地址:
https://spring.io/tools
下载对应操作系统的版本,解压缩后即可使用,需要进行简单设置,方法同1.7节介绍。
如果开发人员本地已有了Eclipse,不想重新下载,则在Eclipse中,选择菜单Help→Install New Software,弹出安装新插件界面,根据Eclipse版本,输入下载地址,Eclipse和Spring Tool Suite 4版本对应关系示例如表1.1所示。建议下载Eclipse的最新版本,体验性更好。
表1.1 Eclipse和Spring Tool Suite 4版本对应关系示例

比如,本地Eclipse是4.12,则在地址栏中输入:
https://download.springsource.com/release/TOOLS/sts4/update/e4.12/
如图1.28所示,单击Enter键,等待下载列表,直到列表框出现能下载内容列表,选择全部,单击Next按钮,等待系统处理,直到出现下一个页面。

图1.28 安装插件页面
然后,跟着页面一步步操作,直到结束。整个过程视网速情况而有所不同,断网情况不可操作。安装完成后,系统会提示重启软件。
以上两种方法都可获得Spring Tool Suite 4体验。当使用的是集成版时,启动界面如图1.29所示。

图1.29 集成Spring Tool Suite 4启动界面
下面介绍在Eclipse中的简单操作方法。
选择菜单File→New→Other,在新建窗口中选择Spring Starter Project项,单击Next按钮,如图1.30所示。
弹出详细设置页。在该页面填写内容较多,有很多项在前面介绍过,在这里还可选择Jar包管理Type类型,默认是Maven;包类型Packaging列表默认是Jar,以及填写Package等,如图1.31所示。填写完成后,单击Next按钮。

图1.30 选择Spring starter Project

图1.31 Spring Starter Project详细页
接着,选择Spring Boot版本和加载依赖项,在这里默认Spring Boot版本是2.2.6,选择Spring Web Starter后,单击Finish按钮,如图1.32所示。在该页面有一个比较人性化的小细节,即能自动记录经常使用的依赖,方便下次新建时快速选择。

图1.32 选择Spring Boot版本和加载依赖项
操作完成后,系统自动下载依赖项和配置,直到系统显示完成。生成的工程结构如图1.33所示。

图1.33 生成的工程目录
这是一个标准的Java Maven工程,这个工程已能直接运行,右击该工程,在弹出的菜单中选择Run As→Maven builder,弹出运行配置窗口,在Goals输入框中输入spring-boot:run,单击Run按钮,运行结束后,Console窗口中出现如图1.34所示内容,运行成功。

图1.34 成功运行标志
打开DemoApplication.java文件,查看自动生成内容,如图1.35所示。

图1.35 DemoApplication.java文件
这样一个快速生成简单Spring Boot的工程模板,可让开发人员快速上手,尤其是前面的新建向导非常有用。Spring Tool Suite 4还提供了一个非常有用的Boot仪表盘(Boot DashBoard),如图1.36所示。
通过该仪表盘,可以快速启动、重启、标记和检查Spring Boot项目。如果该仪表盘没有在工作区,则选择菜单Window→Show view→Other,弹出显示视图窗口,在搜索输入框中输入boot,快速查找Boot Dashboard,如图1.37所示。

图1.36 Boot仪表盘

图1.37 选择Boot仪表盘
选择Boot Dashboard项后,单击Open按钮,则工作区中将显示Boot仪表盘,便于操作。
如果开发人员没有使用Eclipse,还可以选择Visual Studio Code、Atom等工具,同样可以集成Spring Tool Suite 4。