全部
常见问题
产品动态
精选推荐

Spring Boot快速开发利器:Spring Boot CLI

管理 管理 编辑 删除

Spring Boot CLI(Command Line Interface)是一个命令行工具,您可以用它来快速构建Spring原型应用。通过Spring Boot CLI,我们可以通过编写Groovy脚本来快速的构建出Spring Boot应用,并通过命令行的方式将其运行起来。下来,我们来学习一下如何安装和使用Spring Boot CLI。

#安装Spring Boot CLI

关于Spring Boot CLI的安装方式有很多,这里根据目前主要主流的开发平台,具体介绍一下Windows和Mac下的安装方式:

#通用安装

先介绍一个所有平台都可以使用的安装方法。

第一步:下载Spring Boot CLI的工具包:

第二步:解压下载内容,可以看到bin目录下已经有适用于windows和linux平台的两个可执行文件了,我们已经可以直接使用它;为了更方便的使用Spring Boot CLI的命令,我们可以将上面bin目录中对应的可执行文件加入到当前系统的环境变量即可。

#Mac OSX Brew安装

在Mac OSX系统下面就非常方便了,我们可以通过Brew来进行安装,只需要分别执行下面的两条的命令即可:

$ brew tap pivotal/tap
$ brew install springboot

#验证安装

不论使用哪种方法安装,在安装好之后,我们可以通过下面的命令来验证一下当前的安装结果:

$ spring --version
Spring CLI v2.0.0.RELEASE

#运行Groovy脚本

在完成了Spring Boot CLI的安装之后,我们来试试使用它来快速的构建一个Spring Boot应用,方法很简单,只需要如下操作:

第一步:新建一个Groovy脚本,hello.groovy,内容如下:

@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        "Hello World!"
    }
    
}

第二步:使用spring命令运行该Groovy脚本,具体如下:

$ spring run hello.groovy
Resolving dependencies...............................................




                                *::::::::::::::*
                           ::::::::::::::::::::::::::
                        ::::::::::::::::::::::::::::::::       :.
                     *:::::::::::::::::::::::::::::::::::::   :*:
                   :::::::::::::::::::::::::::::::::::::::::::  *:
                 .::::::::::::::::::::::::::::::::::::::::::     :
                ::::::::::::::::::::::::::::::::::::::::::.      .:
               :::::::::::::::::::::::::::::::::::::::::          :
              ::::::::::::::::::::::::::::::::::::::              *:
             :::::::::::::::::::::::::::::::::*                    :
            :::::::::::::::::::*                                   :
            :::::::::::::::                                 *      *:
           .::::::::::::                                   *       .:
           :::::::::::.                                   :         :
           ::::::::::                                    :         .:
           ::::::::::                                  ::          .:
           .::::::::                                 :::           :*
            ::::::::                               :::             :
            ::::::::                            ::::              ::
             :::::::                         :::::               *:
              :::::::                   *:::::::                ::
               :::::::.          :::::::::::                  :::
                :::::::::::::::::::::::*                    ::::
                 .:::   :::::::::*                      .:::::*
                   :     ::::::::::::::::::::::::::::::::::::
                     . .:::::::::::::::::::::::::::::::::::
                        ::::::::::::::::::::::::::::::::
                           ::::::::::::::::::::::::::
                                *::::::::::::::*



2018-04-04 18:11:20.571  INFO 70477 --- [       runner-0] o.s.boot.SpringApplication               : Starting application on zhaiyongchaodeMacBook-Pro.local with PID 70477 (started by zhaiyongchao in /Users/zhaiyongchao/Downloads)
2018-04-04 18:11:20.574  INFO 70477 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2018-04-04 18:11:20.732  INFO 70477 --- [       runner-0] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3c3ad454: startup date [Wed Apr 04 18:11:20 CST 2018]; root of context hierarchy
2018-04-04 18:11:21.758  INFO 70477 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-04-04 18:11:21.783  INFO 70477 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-04 18:11:21.784  INFO 70477 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.28
2018-04-04 18:11:21.795  INFO 70477 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/zhaiyongchao/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-04-04 18:11:21.849  INFO 70477 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader  : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@5e3b901 class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2018-04-04 18:11:21.884  INFO 70477 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-04 18:11:21.885  INFO 70477 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1153 ms
2018-04-04 18:11:22.042  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-04-04 18:11:22.049  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-04 18:11:22.049  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-04-04 18:11:22.049  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-04 18:11:22.049  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-04 18:11:22.391  INFO 70477 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3c3ad454: startup date [Wed Apr 04 18:11:20 CST 2018]; root of context hierarchy
2018-04-04 18:11:22.489  INFO 70477 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String ThisWillActuallyRun.home()
2018-04-04 18:11:22.495  INFO 70477 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-04 18:11:22.496  INFO 70477 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-04 18:11:22.550  INFO 70477 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-04 18:11:22.550  INFO 70477 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-04 18:11:22.598  INFO 70477 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-04 18:11:22.955  INFO 70477 --- [       runner-0] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-04 18:11:23.004  INFO 70477 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-04 18:11:23.010  INFO 70477 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 17.605 seconds (JVM running for 79.248)


从日志中我们可以看到我们通过Groovy脚本定义的一个简单web应用就构建成功了,可以通过访问localhost:8080来验证一下上面应用提供的接口:

$ curl localhost:8080
Hello World!

是不是非常简单?有木有颠覆您对Java应用的印象?

参考文档:getting-started-installing-spring-boot

请登录后查看

CRMEB 最后编辑于2025-02-24 15:25:37

快捷回复
回复
回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
排序 默认正序 回复倒序 点赞倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推荐': '推荐'}}
{{item.is_suggest == 1? '取消推荐': '推荐'}}
沙发 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暂无简介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
290
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

快速安全登录

使用微信扫码登录
{{item.label}} 加精
{{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 源码下载 CRMEB帮助文档 帮助文档
返回顶部 返回顶部
CRMEB客服