概述

通常,构建一个Spring Boot项目有如下方式

  • 使用IDE (比如Spring Tool Suite, IntelliJ IDEA )生成 Starter 代码;

  • 借助 Spring 站点,从 http://start.Spring.io 上下载可配置的 ZIP 文件;

  • 使用到 http://start.Spring.io 站点的 curl 命令来达到相同的效果

方便起见,我们直接使用IDE开发Spring Boot 工程即可。


使用Spring Tool Suite构建Spring Boot项目

下载STS

访问 https://Spring.io/tools/sts/all,并下载 STS 的最新发布版


插件安装

构建第一个 Spring Boot 项目之前,首先需要安装 Gradle 对 STS 的支持。

在 Dashboard 中,可以看到“Manage IDE Extensions”按钮,然后,需要在“Language and framework tooling”区域中选择下载“Gradle Support”。

Spring Boot-Spring Tool Suit  + Gradle 构建第一个Spring Boot 项目01_spring

Spring Boot-Spring Tool Suit  + Gradle 构建第一个Spring Boot 项目01_spring mvc_02

Spring已经不推荐使用该插件管理Gradle项目了,推荐使用BuildShip,暂未找到如何安装,先使用该插件。

同时推荐下载“Groovy Eclipse”以及“Groovy 2.4 compiler”,后面使用 geb 构建验收测试时会用到它们

Spring Boot-Spring Tool Suit  + Gradle 构建第一个Spring Boot 项目01_mvc_03

安装后,重启STS即可生效。

如果你关闭了Dashboard页面,可以通过Help | Dashboard 进入Dashboard 页面。


搭建第一个Spring Boot项目

使用“File | New | Spring Starter Project”导航菜单,这里的可选项是与 http://start.Spring.io 相同的,只不过嵌入到了 IDE 中。

Spring Boot-Spring Tool Suit  + Gradle 构建第一个Spring Boot 项目01_spring_04

这是一个 Gradle 应用,会生成 JAR文件并使用 Java 8。

为 Web 应用生成一个 JAR 文件?将 Web 应用打包为JAR 文件,这一点你们可能会觉得有些诡异。尽管仍然可以将其打包为 WAR,但这并不是推荐的实践
在默认情况下,Spring Boot 将会创建一个胖 JAR 包(fat JAR),这个 JAR 包中包含了应用所有的依赖,提供了通过“java-jar”命令便捷启动 Web 应用的方法。 我们的应用将会打包为JAR文件,如果你想创建WAR文件 的话,可以参考http://spring.io/guides/gs/convert-jar-to-war/。

选择正确的信息后,Next,因为我们构建Spring MVC Web项目,因此只需要选择Web即可

Spring Boot-Spring Tool Suit  + Gradle 构建第一个Spring Boot 项目01_spring boot_05

在配置窗口的底部会有一些复选框,代表各种 Spring Boot starter 库。它们是可以添加到构建文件中的依赖项,针对各种 Spring 项目,它们提供了自动配置功能。

现在只关心 Spring MVC,所以只选中 Web 这个复选框。

等待几分钟后,控制台输出如下信息说明成功。

[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      :cleanEclipse
[sts]      :eclipse
[sts] -----------------------------------------------------
Support for clients using a tooling API version older than 3.0 was deprecated and will be removed in Gradle 5.0. You are currently using tooling API version 2.2.1. You should upgrade your tooling API client to version 3.0 or later.
:cleanEclipseClasspath UP-TO-DATE
:cleanEclipseJdt UP-TO-DATE
:cleanEclipseProject UP-TO-DATE
:cleanEclipse UP-TO-DATE
:eclipseClasspath
:eclipseJdt
:eclipseProject
:eclipse

BUILD SUCCESSFUL in 2s
6 actionable tasks: 3 executed, 3 up-to-date
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 0 min, 2 sec
[sts] -----------------------------------------------------

成功后我们将会得到如下的工程结构

Spring Boot-Spring Tool Suit  + Gradle 构建第一个Spring Boot 项目01_mvc_06

  • 主类MasterSpringMvcApplication 及其测试类 MasterSpringMvcApplicationTests

  • 两个空的文件夹 — static 和templates,它们分别用来存放静态Web 资源(图片、样式文件等)和模板(jsp、freemarker 或Thymeleaf)。

  • application.properties,它是 Spring Boot 默认的配置文件,默认是空的

  • 构建文件 build.gradle

启动项目

到这里,我们就可以运行应用的主方法,这样就会启动一个 Web 服务器

运行 MasterSpringMvcApplication#main方法 Run As Spring Boot App

Spring Boot-Spring Tool Suit  + Gradle 构建第一个Spring Boot 项目01_spring boot_07

启动日志


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

2018-03-07 16:14:01.863  INFO 21784 --- [           main] m.MasterSpringMvcApplication             : Starting MasterSpringMvcApplication on Mr with PID 21784 (D:\workspace\workspace-sts-3.9.2\masterSpringMvc\bin\main started by Mr.Yang in D:\workspace\workspace-sts-3.9.2\masterSpringMvc)
2018-03-07 16:14:01.868  INFO 21784 --- [           main] m.MasterSpringMvcApplication             : No active profile set, falling back to default profiles: default
2018-03-07 16:14:01.996  INFO 21784 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2cbb3d47: startup date [Wed Mar 07 16:14:01 CST 2018]; root of context hierarchy
2018-03-07 16:14:03.704  INFO 21784 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-03-07 16:14:03.738  INFO 21784 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-03-07 16:14:03.738  INFO 21784 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.28
2018-03-07 16:14:03.745  INFO 21784 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_161\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_161/bin/server;C:/Program Files/Java/jre1.8.0_161/bin;C:/Program Files/Java/jre1.8.0_161/lib/amd64;C:\ProgramData\Oracle\Java\javapath;E:\Program Files\Java\jdk1.7.0_51\bin;E:\Program Files\Java\jdk1.7.0_51\jre\bin;C:\windows\system32\;E:\Program Files\Python27;E:\Program Files\Git\cmd;E:\libs\apache-maven-3.5.0-bin\apache-maven-3.5.0\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Users\Mr.Yang\AppData\Local\Microsoft\WindowsApps;C:\Users\Mr.Yang\AppData\Local\GitHubDesktop\bin;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;;E:\Program Files\spring-tool-suite-3.9.2.RELEASE\sts-3.9.2.RELEASE;;.]
2018-03-07 16:14:03.928  INFO 21784 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-03-07 16:14:03.929  INFO 21784 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1938 ms
2018-03-07 16:14:04.227  INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-03-07 16:14:04.235  INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-07 16:14:04.236  INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-03-07 16:14:04.236  INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-03-07 16:14:04.236  INFO 21784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-03-07 16:14:04.811  INFO 21784 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2cbb3d47: startup date [Wed Mar 07 16:14:01 CST 2018]; root of context hierarchy
2018-03-07 16:14:04.891  INFO 21784 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-03-07 16:14:04.894  INFO 21784 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-03-07 16:14:04.952  INFO 21784 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-07 16:14:04.952  INFO 21784 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-07 16:14:05.035  INFO 21784 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-07 16:14:05.344  INFO 21784 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-07 16:14:05.405  INFO 21784 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-03-07 16:14:05.412  INFO 21784 --- [           main] m.MasterSpringMvcApplication             : Started MasterSpringMvcApplication in 4.06 seconds (JVM running for 5.889)

此时,tomcat容器已经启动成功,访问 http://localhost:8080,此时会产生一个错误,不必担心,写个Controller,随便提供个方法,访问该方法即可

Spring Boot-Spring Tool Suit  + Gradle 构建第一个Spring Boot 项目01_mvc_08