Spring Boot CLI 设置和 HelloWorld 示例

在我之前的帖子中(Spring Boot Introduction)(/社区/教程/Spring Boot-教程)和(Spring Boot Components and Internals)(/社区/教程/key-components-and-internals-of-spring-boot-framework),我们已经讨论了Spring Boot的基础知识和四大Spring Boot组件的使用。

什么是 Spring Boot CLI?

Spring Boot CLI(命令线接口)是一个 Spring Boot 软件,可以运行和测试 Spring Boot 应用程序从命令提示. 当我们使用 CLI 运行 Spring Boot 应用程序时,它会内部使用 Spring Boot Starter 和 Spring Boot AutoConfigurate 组件来解决所有依赖并执行应用程序。

Spring Boot CLI 安装

我們可以使用 Windows 安裝程式或 Zip 檔案安裝 Spring Boot CLI 軟體。 兩種方法都很容易安裝,並將給我們相同的 Spring Boot CLI 軟體。 我們將使用使用使用 Zip 檔案的簡單方法。 我們將使用 Spring Boot 最新版本: 1.2.3.RELEASE 我們可以下載 Spring Boot CLI 軟體在: https://start.spring.io/ (這是 Spring Initilizr WebInterface. 我们將詳細討論這個組件在即將發表的文章中)。

  • 下载 Spring Boot CLI zip 文件使用 Spring Initilizr

Click on "Download Spring CLI Zip" button as shown below: download-springboot-cli Once we download Spring Boot CLI Zip file into our local FileSystem, it looks like this. springboot-cli-software- Extract spring-boot-cli-1.2.3.RELEASE.zip file into our local FileSystem. springboot-cli-home- Set Spring Boot CLI Environment Variables in Windows System as shown below.

1set PATH=D:\spring-boot-cli-1.2.3.RELEASE\bin;%PATH%
  • 执行下面的命令来验证我们的安装过程

springboot-cli-version We can use "spring --version" to know the Spring Boot CLI Version as shown below.

1spring --version

我们可以使用春季 - 帮助来了解如下所示的春季启动 CLI 版本。

1spring --help

现在我们的Spring Boot CLI安装过程已经成功完成,在讨论Spring Boot HelloWorld 示例之前,我们首先将看到如何从命令提示中运行 Groovy 脚本。

开启春天命令

Spring Boot CLI 软件提供了一个春季命令,可以从命令提示程序运行 Spring Boot Groovy 脚本. 正如我们之前所看到的那样,春季启动春季 - 帮助命令有许多选项来使用这个命令用于不同的目的。

1spring run <SpringBoot-Groovy-Scriptname>

这里是 Spring Boot 应用程序的 Groovy Script 文件名。我们将使用这个命令来执行我们的 Spring Boot HelloWorld 示例。 现在是时候使用 Spring Boot CLI 使用 Simple HelloWorld 示例工作了。

春天博客HelloWorld例子

现在我们将开发一个 Spring Boot MVC RestController 示例,这是 Pivotal 团队在 Twitter 上发布的第一个示例,展示了 Spring Boot Framework 的功能。

  • 在我们的本地文件系统中创建一个HelloWorld文件夹,将我们的 groovy 脚本放置 *使用以下内容开发一个 Groovy 脚本文件
1@RestController
2class HelloWorld {
3  @RequestMapping("/")
4  String hello() {
5    "Hello JournalDev World."
6  }
7}

将此文件命名为 HelloWorld.groovy. 在这里 ".groovy" 扩展是强制性的。 代码解释

  • 使用 Spring 4 MVC @RestController 注释定义了 REST 控制器
  • 使用 Spring MVC @RequestMapping 注释定义了Mapping URL /`
  • 定义了返回字符串到客户端或 Web 浏览器的方法

代码观察 如果我们观察我们的HelloWorld.groovy,我们可以找到以下重要的点。

  • 没有导入
  • 没有其他 XML 配置来定义 Spring MVC 组件,如 View,ViewResolver 等
  • 没有 web.xml 和没有 DispatcherServlet 声明
  • 没有构建脚本来创建我们的应用程序战争文件
  • 无需构建战争文件来部署此应用程序

Then who will provide all these things to our Spring Boot HelloWorld application? First run the application and see the results, then we will answer this question.- Now Spring Boot HelloWorld Example folder looks like this. springboot-cli-helloworld

现在我们的 Spring Boot HelloWorld 示例已准备好使用 Spring MVC RestController. 是时候运行和测试这个示例来了解 Spring Boot 框架的力量了。

运行春天机器人HelloWorld示例

请按照以下步骤测试我们的 Spring Boot HelloWorld 示例应用程序:

  • 在我们的本地文件系统中打开HelloWorld文件夹的命令提示 *执行以下命令
1spring run HelloWorld.groovy
  • 观察春运指令控制台的输出

springboot-cli-start If we observe here, when we execute "spring run HelloWorld.groovy", it starts Embedded Tomcat server at Default port number: 8080. Now our Spring Boot HelloWorld Example application is up and running. It's time to test it now. NOTE:- If we observe the above screen shot, I have highlighted "SpringApplication" class file. Here o.s.boot.SpringApplication means org.springframework.boot.SpringApplication class. What is this SpringApplication? What is the use of SpringApplication? Please refer my upcoming posts to answer these questions.- Open browser and access the following link. Access this URL: https://localhost:8080/ springboot-helloworld-output Now we are able to access our first Spring Boot MVC RESTful WebService.

如果我们观察这个 Spring Boot 应用程序,那么我们可能会想到这个问题:谁会为我们的 Spring Boot HelloWorld 应用程序提供所有这些东西?

  • 没有导入
  • 没有其他 XML 配置来定义 Spring MVC 组件,如 View,ViewResolver 等
  • 没有 web.xml 和没有 DispatcherServlet 声明
  • 没有构建脚本来创建我们的应用程序战争文件
  • 无需构建战争文件来部署此应用程序

回答这个问题: 这是 Spring Boot Core Components, Groovy Compiler (groovyc) 和 Groovy Grape (Groovy’s JAR Dependency Manager) 的责任。 Spring Boot Components 使用 Groovy Boot Compiler 和 Groovy Grape 提供一些默认的粘土添加所需的进口,提供所需的配置,解决 jar 依赖,添加 main() 方法等。 作为一个 Spring Boot 开发人员,我们不需要担心所有这些事情。 Spring Boot Framework 将为我们照顾所有这些事情。 这是 Spring Boot Framework 的美丽。 因此, Spring Boot Framework 避免了大量的 boilerplate 代码和 Spring Boot 配置,减少了开发时间和提高了生产力。 在这里,我们没有讨论 Spring Boot 注释,

Published At
Categories with 技术
Tagged with
comments powered by Disqus