
1.5 Spring Boot 2示例
本节将介绍一个基于Spring Boot 2的简单Web项目。首先,在Eclipse的菜单中,选择File→New→Project,在弹出窗口中,选择Maven Project项,然后单击Next按钮。
在新打开的窗口中勾选第一项,即创建一个简单项目,然后单击Next按钮,打开一个新窗口,如图1.13所示。填写Group Id和Artifact Id项,然后单击Finish按钮,完成新项目的创建。

图1.13 New Maven项目窗口
在新建项目中打开pom.xml文件,增加如下内容:

以上内容很简洁,元素properties中内容是为了解决Eclipse存在的一个bug,但不影响项目运行。元素parent和dependency属于核心依赖,在这里使用了版本2.2.6.RELEASE。保存该pom文件后,在路径src\main\java\com\zioer\controller创建文件SpringBootApp.java,内容如下:

保存以上内容,该java类文件很简单,一个main函数和一个普通方法,运行该文件,在Console窗口出现如图1.14所示内容,并很快启动完成。

图1.14 Console窗口
在浏览器中输入下列网址:
http://127.0.0.1:8080/index
访问可得到如图1.15所示内容。

图1.15 访问Spring Boot简单项目
启动并运行成功。如图1.16所示为该项目工程目录。
该项目的Maven依赖如图1.17所示。
图1.17列出的只是其中一部分,实际上该项目依赖的Jar包很多。由以上示例可知,创建Spring Boot项目非常简单,很容易构建一个基本框架,并解决基于Spring Boot依赖问题,其重点在于(POM)文件中的starter依赖,给开发人员减负,开发人员无需关注太多东西,只需要关注其中一小部分内容即可。

图1.16 springbootdemo目录

图1.17 项目依赖
实际上,Spring Boot提供了很多这样类似的starter依赖,常见的有:
· spring-boot-starter-web;
· spring-boot-starter-data-jpa;
· spring-boot-starter-security;
· spring-boot-starter-test;
· spring-boot-starter-thymeleaf;
· spring-boot-starter-jdbc;
· spring-boot-starter-json。
本节介绍Spring Boot的一个简单示例,实现了一个简单功能,篇幅不多,由此可见,Spring Boot实现一个开发应用系统基本框架,没有复杂的XML配置,需编写pom文件和一个java类,便能成功运行。