Springbootcli 简明教程

Spring Boot CLI - Starter Thymeleaf Project

让我们创建一个基于 Thymeleaf 的示例项目来演示 Spring CLI 的功能。请按照以下步骤创建示例项目。

Step

Description

1

创建一个名为 TestApplication 的文件夹,其中包含模板和静态子文件夹。

2

在 TestApplication 文件夹中创建 message.groovy,在模板文件夹中创建 message.html,在静态文件夹中创建 index.html,如下所述。

3

编译并运行应用程序以验证已实现逻辑的结果。

TestApplication/message.groovy

@Controller
@Grab('spring-boot-starter-thymeleaf')
class MessageController {
   @RequestMapping("/message")
   String getMessage(Model model) {
      String message = "Welcome to TutorialsPoint.Com!";
      model.addAttribute("message", message);
      return "message";
   }
}

TestApplication/templates/message.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
   <head>
      <title>Spring Boot CLI Example</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   </head>
   <body>
      <p th:text="'Message: ' + ${message}" />
   </body>
</html>

TestApplication/static/index.html

<!DOCTYPE HTML>
<html>
   <head>
      <title>Spring Boot CLI Example</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   </head>
   <body>
      <p>Go to <a href="/message">Message</a></p>
   </body>
</html>

Run the application

键入以下命令

E:/Test/TestApplication/> spring run *.groovy

现在 Spring Boot CLI 将发挥作用,下载所需的依赖项,运行嵌入式 Tomcat,部署应用程序并启动它。您可以在控制台上看到以下输出。

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

2022-02-03 11:36:33.362  INFO 10764 --- [       runner-0] o.s.boot.SpringApplication               : Starting application using Java 11.0.11 on DESKTOP-86KD9FC with PID 10764 (started by intel in E:\Test\TestApplication)
2022-02-03 11:36:33.372  INFO 10764 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2022-02-03 11:36:35.743  INFO 10764 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-02-03 11:36:35.768  INFO 10764 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-02-03 11:36:35.769  INFO 10764 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-03 11:36:35.829  INFO 10764 --- [       runner-0] org.apache.catalina.loader.WebappLoader  : Unknown class loader [org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@553a3d88] of class [class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader]
2022-02-03 11:36:35.896  INFO 10764 --- [       runner-0] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-02-03 11:36:35.897  INFO 10764 --- [       runner-0] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2085 ms
2022-02-03 11:36:36.436  INFO 10764 --- [       runner-0] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2022-02-03 11:36:37.132  INFO 10764 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-02-03 11:36:37.153  INFO 10764 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 4.805 seconds (JVM running for 8.633)
2022-02-03 11:37:03.049  INFO 10764 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-02-03 11:37:03.049  INFO 10764 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-02-03 11:37:03.054  INFO 10764 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 3 ms

Browse the application in Browser

基于弹簧的休息应用程序现已准备就绪。将 url 打开为“http://localhost:8080/”,您将看到以下输出。

Go to Message

点击消息链接,您将看到以下输出。

Message: Welcome to TutorialsPoint.Com!

Points to consider

Spring CLI 执行以下操作。

  1. @Grab('spring-boot-starter-thymeleaf') 注解指示 CLI 下载 spring-boot-starter-thymeleaf 2.6.3 版本。

  2. Spring CLI 使用其元数据自动检测版本,因为我们此处未指定任何组 ID 或版本 ID。

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