Spring Boot CLI DEMO Setup

来源:互联网 发布:mac淘宝卖家 编辑:程序博客网 时间:2024/06/05 03:10

这几天在做Spring Boot的学习,看了官方文档,想到搭建一个小Demo来体验一下Spring Boot的强大,然而昨天搭建失败了(原因还是英文太烂,有些步骤根本看不懂。。。)。

今早过来,看了一些其他的文档,终于成功了!!!下面来做一些总结并分享一下步骤:

What is Spring Boot CLI?

Spring Boot CLI(Command Line Interface) is a Spring Boot software to run and test Spring Boot applications from command prompt. When we run Spring Boot applications using CLI, then it internally uses Spring Boot Starter and Spring Boot AutoConfigurate components to resolve all dependencies and execute the application.

It internally contains Groovy and Grape (JAR Dependency Manager) to add Spring Boot Defaults and resolve all dependencies automatically.

We are going to discuss about CLI Installation, CLI Setup and CLI commands in Windows Environment. It’s almost similar to other Environment too.

Spring Boot CLI Installation

We can install Spring Boot CLI software using either Windows Installer or Zip file. Both approaches are easy to install and will give us same Spring Boot CLI software. We are going to use easy method that is using Zip file. We are going to use Spring Boot Latest version: 1.4.0.RELEASE

zip安装包下载地址:http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/

本文所用的1.4.0版本下载地址:http://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/1.4.0.BUILD-SNAPSHOT/spring-boot-cli-1.4.0.BUILD-SNAPSHOT-bin.zip

Please follow these steps to Install and setup Spring Boot CLI software in Windows Environment.
下载完成后解压,如图所示:

下载完成示例

Set Spring Boot CLI Environment Variables in Windows System as shown below.

Environment Variables

No specific environment variables are required to run the CLI, however, you may want to
set SPRING_HOME to point to a specific installation. You should also add SPRING_HOME/bin
to your PATH environment variable.

本人昨天看的文档,当时并没有看懂。。。然后就一直失败

最重要的一步:配置系统环境变量

set PATH=D:\develop\spring boot\spring-1.4.0.BUILD-SNAPSHOT\bin
环境变量配置

然后查看一下有没有配成功
We can use “spring –version” to know the Spring Boot CLI Version as shown below.

spring –version

We can use “spring –help” to know the Spring Boot CLI Version as shown below.

spring –help

spring --version

如图所示,输入spring –version(注意输入的是- -,两个横杠,中间没有空格,一起输入不加空格,显示成一条了) 显示版本spring-1.4.0.BUILD-SNAPSHOT

Now our Spring Boot CLI Installation process is done successfully.

Before discussing about Spring Boot “HelloWorld” Example, first we will see how to run Groovy scripts from command prompt.
Spring Boot “spring” command

Spring Boot CLI Software provides a “spring” command to run Spring Boot Groovy scripts from Command Prompt. As we saw just before, Spring Boot “spring –help” command have many options to use this command for different purposes. One important option, we are going to use here is “run” option.

“spring” command syntax

spring run <SpringBoot-Groovy-Scriptname>

Spring Boot HelloWorld Example

Now we are going to develop a Spring Boot MVC RestController Example. It was the first example published on Twitter from Pivotal team to demonstrate Spring Boot Framework power.

Please follow the following steps to develop a Spring Boot HelloWorld Example:

Create a “HelloWorld” Folder in our Local FileSystem to place our groovy scripts.Develop a Groovy script file using the following content
@RestControllerclass HelloWorld {  @RequestMapping("/")  String hello() {    "Hello, my first Spring Boot Demo."  }}

Name this file as HelloWorld.groovy. Here “.groovy” extension is mandatory.

这里写图片描述

如图所示,我在d盘新建了一个文件夹HelloWorld,然后新建了一段groovy代码。

Code Explanation

Defined a REST Controller using Spring 4 MVC @RestController annotation.Defined a Mapping URL “/jun” using Spring MVC @RequestMapping annotation.Defined a method to return a String to a Client or Web Browser.

Code Observations

If we observe our HelloWorld.groovy, we can find the following important points.

No importsNo other XML configuration to define Spring MVC Components like Views,ViewResolver etc.No web.xml and No DispatcherServlet declarationNo build scripts to create our Application war fileNo need to build war file to deploy this application

什么导入,什么配置文件都没有。

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 our Spring Boot HelloWorld Example is ready with Spring MVC RestController. It’s time to run and test this example to know the Power of Spring Boot Framework.

Run Spring Boot HelloWorld Example

Please follow the following steps to test our Spring Boot HelloWorld Example application:

Open command prompt at “HelloWorld” Folder in our Local FileSystem.Execute the following command
spring run HelloWorld.groovy
Observe the output at “spring run” command console.

先进入d盘下面HelloWorld文件夹,然后输入上述代码。观察输出

这里写图片描述

第一次启动的时候会比较慢,输入框里面显示 Resolving dependencies……(应该是spring boot在下载一个相关配置)

If we observe here, when we execute “spring run HelloWorld.groovy”, it starts Embedded Tomcat server at Default port number: 8080.

同时可以看到,它自动启动了tomcat并设置端口号为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: http://localhost:8080/jun

demo

没错,就是这么神奇!!!

Now we are able to access our first Spring Boot MVC RESTful WebService.

If we observe this Spring Boot application, then we may get this question in our mind: who will provide all these things to our Spring Boot HelloWorld application?

No importsNo other XML configuration to define Spring MVC Components like Views,ViewResolver etc.No web.xml and No DispatcherServlet declarationNo build scripts to create our Application war fileNo need to build war file to deploy this application

Answer to this question: It is the responsibility of Spring Boot Core Components, Groovy Compiler (groovyc) and Groovy Grape (Groovy’s JAR Dependency Manager).

Spring Boot Components uses Groovy Compiler and Groovy Grape to provide some Defaults lime adding required imports, providing required configuration, resolving jar dependencies, adding main() method etc. As a Spring Boot Developer, We don’t need to worry all these things. Spring Boot Framework will take care of all these things for us.

That’s the beauty of Spring Boot Framework.

By this way, Spring Boot framework avoids lots of boilerplate code and Spring Configuration, reduces development time and increases productivity.

Here we have not discussed much about Spring Boot Annotations, Spring Boot API, What is the use of main() method in Spring Boot Application etc. We will answer all these questions in coming posts using Spring Boot IDEs.

That’s it about Spring Boot CLI. If you have any questions, please drop a comment.

好了,到这里结束了,一个spring boot cli 的demo就完成了。

原文地址:http://www.journaldev.com/8195/spring-boot-cli-setup-and-helloworld-example

0 0
原创粉丝点击