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
,内容如下:
@RestControllerclass 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$DefaultScopeParentClassLoader2018-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
作者:程序猿DD
链接:https://www.jianshu.com/p/e0ba66bf7ce7
共同学习,写下你的评论
评论加载中...
作者其他优质文章