Spring Boot CLI——預設語句



預設匯入項

Spring CLI 預設自動匯入眾多庫,因此不需要顯式匯入。請考慮以下 groovy 指令碼。

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

這裡包含的 @RestController 和 @RequestMapping 註解的匯入內容已經由 Spring Boot 預設包含。我們甚至不需要使用完全限定名稱。可以透過執行應用程式來檢查。

輸入以下命令

E:/Test/> spring run FirstApplication.groovy

可以在控制檯上看到如下輸出。

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

2022-02-03 11:29:01.177  INFO 10668 --- [       runner-0] o.s.boot.SpringApplication               : Starting application using Java 11.0.11 on DESKTOP-86KD9FC with PID 10668 (started by intel in F:\Test)
2022-02-03 11:29:01.187  INFO 10668 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2022-02-03 11:29:03.555  INFO 10668 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-02-03 11:29:03.591  INFO 10668 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-02-03 11:29:03.592  INFO 10668 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-03 11:29:03.659  INFO 10668 --- [       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:29:03.735  INFO 10668 --- [       runner-0] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-02-03 11:29:03.736  INFO 10668 --- [       runner-0] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2107 ms
2022-02-03 11:29:04.945  INFO 10668 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-02-03 11:29:04.968  INFO 10668 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 4.811 seconds (JVM running for 8.805)

自動 Main 方法

我們不需要為 groovy 指令碼建立標準 main 方法來初始化 spring 應用程式,它將自動為 spring boot 應用程式建立。

廣告