Spring Boot CLI - “Hello World” 示例



在此示例中,我們將建立一個基於 Spring Boot + MVC + Rest 的 Web 應用程式。

步驟 1:建立原始檔夾

在 E:\Test 資料夾中建立一個名為 FirstApplication 的資料夾。

步驟 2:建立原始檔

在 E:\Test 資料夾中使用以下原始碼建立 FirstApplication.groovy 檔案。

@RestController
class FirstApplication {
   @RequestMapping("/")
   String welcome() {
      "Welcome to TutorialsPoint.Com"
   }
}

步驟 3:執行應用程式

輸入以下命令

E:/Test/> spring run FirstApplication.groovy

Spring Boot CLI 現在生效,下載所需的依賴項、執行嵌入式 Tomcat、部署應用程式並啟動它。您可以在控制檯上看到以下輸出。

E:\Test>spring run FirstApplication.groovy
Resolving dependencies...............................

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

2022-02-03 11:12:42.683  INFO 6956 --- [       runner-0] o.s.boot.SpringApplication               : Starting application using Java 11.0.11 on DESKTOP-86KD9FC with PID 6956 (started by intel in F:\Test)
2022-02-03 11:12:42.710  INFO 6956 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2022-02-03 11:12:45.110  INFO 6956 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-02-03 11:12:45.138  INFO 6956 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-02-03 11:12:45.139  INFO 6956 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-03 11:12:45.229  INFO 6956 --- [       runner-0] org.apache.catalina.loader.WebappLoader  : Unknown class loader [org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@8646db9] of class [class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader]
2022-02-03 11:12:45.333  INFO 6956 --- [       runner-0] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-02-03 11:12:45.333  INFO 6956 --- [       runner-0] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2124 ms
2022-02-03 11:12:46.901  INFO 6956 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-02-03 11:12:46.930  INFO 6956 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 5.416 seconds (JVM running for 49.049)
2022-02-03 11:13:48.910  INFO 6956 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-02-03 11:13:48.912  INFO 6956 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-02-03 11:13:48.915  INFO 6956 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 3 ms

步驟 4:在瀏覽器中瀏覽應用程式

基於 Spring 的 rest 應用程式現在已準備好。開啟 url “https://:8080/”,您將看到以下輸出。

Welcome to TutorialsPoint.Com

注意事項

Spring CLI 執行以下操作。

  • 僅首次下載所有依賴項 JAR。

  • Spring CLI 根據程式碼中使用的類和註釋自動檢測要下載的依賴項 JAR。

  • 最後,它編譯程式碼,將 war 部署在嵌入式 Tomcat 中,在預設埠 8080 上啟動嵌入式 Tomcat 伺服器。

廣告