Spring Boot教學
Spring Boot是什麼?
Spring Boot簡介
Spring Boot主要目標
Spring Boot快速入門
新項目爲什麼需要Spring Boot?
Spring Boot引導過程
Spring Boot核心和限制
Spring Boot Tomcat部署
Spring Boot優點和缺點
Spring Boot構建系統
Spring Boot入門
Spring Boot代碼結構
Spring Boot安裝
Spring Boot Bean和依賴注入
Spring Boot應用程序開發入門
Spring Boot運行器(Runner)
Spring Boot JSP應用實例
Spring Boot應用程序屬性
Spring Boot將WAR文件部署到Tomcat
Spring Boot日誌
Spring Boot Hello World(Thymeleaf)示例
Spring Boot構建RESTful Web服務
Spring Boot非web應用程序實例
Spring Boot異常處理
Spring Boot @ConfigurationProperties實例
Spring Boot攔截器
Spring Boot SLF4J日誌實例
Spring Boot Servlet過濾器
Spring Boot Ajax實例
Spring Boot Tomcat端口號
Spring Boot文件上傳示例(Ajax和REST)
Spring Boot Rest模板
Spring Boot文件上傳示例
Spring Boot文件處理
Spring Boot服務組件
Spring Boot Thymeleaf示例
Spring Boot使用RESTful Web服務
Spring Boot CORS支持
Spring Boot國際化
Spring Boot調度
Spring Boot啓用HTTPS
Spring Boot Eureka服務器
Spring Boost Eureka服務註冊
Spring Boot Zuul代理服務器和路由
Spring Boot雲配置服務器
Spring Boot雲配置客戶端
Spring Boot Actuator
Spring Boot管理服務器
Spring Boot管理客戶端
Spring Boot啓用Swagger2
Spring Boot創建Docker鏡像
Spring Boot跟蹤微服務日誌
Spring Boot Flyway數據庫
Spring Boot發送電子郵件
Spring Boot Hystrix
Spring Boot Web Socket
Spring Boot批量服務
Spring Boot Apache Kafka
Spring Boot單元測試用例
Spring Boot Rest控制器單元測試
Spring Boot數據庫源(連接數據庫)
Spring Boot保護Web應用程序

Spring Boot快速入門

本章將演示如何使用Maven和Gradle創建Spring Boot應用程序。

前提條件

系統需要具有以下創建Spring Boot應用程序的最低要求 -

  • Java 7
  • Maven 3.2
  • Gradle 2.5

Spring Boot CLI

Spring Boot CLI是一個命令行工具,它用於運行Groovy腳本。它是使用Spring Boot命令行界面創建Spring Boot應用程序的最簡單方法。可以在命令提示符下創建,運行和測試應用程序。

本節介紹手動安裝Spring Boot CLI所涉及的步驟。 如需進一步的幫助,可以使用以下鏈接:

還可以從Spring Software存儲庫下載Spring CLI發行版:

對於手動安裝,需要使用以下兩個文件夾 -

  • spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.zip
  • spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.tar.gz

下載後,解壓縮歸檔文件並按照install.txt文件中給出的步驟進行操作。 並不是說它不需要任何環境設置。
在Windows中,轉到命令提示符下的Spring Boot CLI bin目錄並運行命令spring --version以確保正確安裝了spring CLI。 執行命令後,可以看到spring CLI版本,如下所示 -

Spring

使用Groovy運行Hello World

創建一個包含Rest Endpoint腳本的簡單groovy文件:hello.groovy,並使用spring boot CLI運行groovy文件。代碼如下所示 -

@Controller
class Example {
   @RequestMapping("/")
   @ResponseBody
   public String hello() {
      "Hello Spring Boot"
   }
}

請注意,在這個示例中,已經配置Spring Boot CLI bin目錄到系統環境變量,並把文件:hello.groovy保存到D:/worksp/springboot/目錄。使用命令spring run hello.groovy運行應用程序,如下面所示 -

D:\worksp\springboot>spring run hello.groovy

執行結果如下所示:

D:\worksp\springboot>spring run hello.groovy

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

2018-09-26 09:34:13.782  INFO 13908 --- [       runner-0] o.s.boot.SpringApplication               : Starting application on DESKTOP-CAN8JLM with PID 13908 (started by hema in D:\worksp\springboot)
2018-09-26 09:34:13.810  INFO 13908 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2018-09-26 09:34:14.747  INFO 13908 --- [       runner-0] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ec39a62: startup date [Wed Sep 26 09:34:14 CST 2018]; root of context hierarchy
2018-09-26 09:34:18.655  INFO 13908 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-09-26 09:34:18.735  INFO 13908 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-09-26 09:34:18.736  INFO 13908 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-09-26 09:34:18.756  INFO 13908 --- [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 (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\PuTTY\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\WINDOWS\System32\OpenSSH\;D:\wamp\bin\php\php7.0.29;C:\Program Files (x86)\nodejs\;D:\software\spring-2.0.5.RELEASE\bin;D:\Program Files\Python3\Scripts\;D:\Program Files\Python3\;C:\Users\hema\AppData\Local\Microsoft\WindowsApps;C:\Users\hema\AppData\Roaming\npm;.]
2018-09-26 09:34:19.030  INFO 13908 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader  : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@24ef272d class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2018-09-26 09:34:19.140  INFO 13908 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-09-26 09:34:19.140  INFO 13908 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4398 ms
2018-09-26 09:34:19.303  INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-09-26 09:34:19.311  INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-26 09:34:19.318  INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-26 09:34:19.320  INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-26 09:34:19.322  INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-26 09:34:19.556  INFO 13908 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-26 09:34:19.831  INFO 13908 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ec39a62: startup date [Wed Sep 26 09:34:14 CST 2018]; root of context hierarchy
2018-09-26 09:34:19.998  INFO 13908 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String Example.hello()
2018-09-26 09:34:20.004  INFO 13908 --- [       runner-0] 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-09-26 09:34:20.006  INFO 13908 --- [       runner-0] 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-09-26 09:34:20.050  INFO 13908 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-26 09:34:20.053  INFO 13908 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-26 09:34:20.651  INFO 13908 --- [       runner-0] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-09-26 09:34:20.821  INFO 13908 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-26 09:34:20.830  INFO 13908 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 9.092 seconds (JVM running for 15.353)

運行groovy文件,所需的依賴項將自動下載,它將在Tomcat 8080端口啓動應用程序,如下面給出的屏幕截圖所示 -
Tomcat啓動後,轉到Web瀏覽器並點擊URL => http://localhost:8080/,可以看到輸出,如圖所示:

Spring