Springbootcli 简明教程

Spring Boot CLI - Hello World Example

在此示例中,我们将创建一个基于 Spring Boot + MVC + Rest 的 Web 应用程序。

Step 1: Create source Folder

在 E:\Test 文件夹中创建 FirstApplication 文件夹。

Step 2: Create Source File

在 E:\Test 文件夹中使用以下源代码创建 FirstApplication.groovy 文件。

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

Step 3: Run the application

键入以下命令

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

Step 4: Browse the application in Browser

基于 Spring 的休息应用程序现已准备就绪。打开 URL " http://localhost:8080/ ",您将看到以下输出。

Welcome to TutorialsPoint.Com

Points to consider

Spring CLI 执行以下操作。

  1. 所有依赖项 JAR 只下载一次。

  2. Spring CLI 根据代码中使用的类和注释自动检测要下载哪些依赖项 JAR。

  3. 最后,它编译代码,将 war 部署到嵌入式 Tomcat 上,在默认端口 8080 上启动嵌入式 Tomcat 服务器。