
1.3 SpringBoot编程起步
SpringBoot编程需要依赖于Maven或Gradle构建工具完成,这里将直接使用Maven进行开发,同时利用Eclipse来建立Maven项目。
1.在Eclipse中创建一个新的Maven项目,项目类型为quickstart,如图1-4所示。

图1-4 Eclipse创建Maven项目
2.设置Maven项目的信息(Group Id、Artifact Id、Version等),本例建立的项目名称为bootstart,如图1-5所示。

图1-5 设置Maven的配置信息
3.修改pom.xml配置文件,追加SpringBoot的依赖配置与相关插件。

本程序采用官方文档给出的配置方式实现了SpringBoot项目的创建。这里,spring-boot-starter-parent就是官方给出的快速构建SpringBoot项目的公共父pom.xml配置文件支持。
注意:配置完成后要更新项目。
本例的项目开发是基于Eclipse完成的,开发者修改完pom.xml配置文件之后,一定要更新项目(快捷键为Alt + F5),如图1-6所示。

图1-6 更新Eclipse项目
4.编写第一个SpringBoot程序。

5.这里使用了Eclipse-STS(Spring Source Tool)插件,所以可以直接运行,如图1-7所示。

图1-7 运行SpringBoot项目
提示:采用Maven工具直接运行。
如果Eclipse工具中没有安装STS开发插件,也可以通过Maven的方式运行。直接输入spring-boot:run,就可以启动SpringBoot项目了,如图1-8所示。

图1-8 使用Maven运行SpringBoot程序
考虑到代码编写的方便,本书强烈建议读者安装STS开发插件。本书中所有的项目也都是通过STS插件的方式开发的。同时需要提醒读者的是,当使用Eclipse-STS建立了SpringBoot项目时,会在项目后面标记“[boot]”提示信息。
6.SpringBoot项目启动之后,开发者可以直接通过控制台看到如图1-9所示的信息提示,完成后的项目结构如图1-10所示。

图1-9 SpringBoot启动提示信息

图1-10 项目结构
同时也可以在控制台中看到如下的日志信息:

由于SpringBoot自带Tomcat容器,所以项目启动后通过浏览器输入http://localhost:8080,就可以直接访问控制器返回的信息,如图1-11所示。

图1-11 SpringBoot运行界面