Spring Boot CLI入门程序

在这个例子中,我们将创建一个基于Spring Boot + MVC + Rest的Web应用程序。

第1步:创建源文件夹

D:\worksp文件夹中创建文件夹FirstApplication

第2步:创建源文件

使用以下源代码在D:\worksp文件夹中创建FirstApplication.groovy文件 -

@RestController
class FirstApplication {
   @RequestMapping("/")
   String welcome() {
      "Welcome to zyiz.net"
   }
}

第3步:运行应用程序

输入以下命令 -

D:\worksp\springboot-cli> spring run FirstApplication.groovy

现在,Spring Boot CLI将开始运行,下载所需的依赖项,运行嵌入的tomcat,部署应用程序并启动它。可以在控制台上看到类似以下输出 -

D:\worksp\springboot-cli>spring run FirstApplication.groovy
Resolving dependencies.................................................

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

2018-09-25 09:29:51.393  INFO 12532 --- [       runner-0] o.s.boot.SpringApplication               : Starting application on DESKTOP-CAN8JLM with PID 12532 (started by hema in D:\worksp\springboot-cli)
2018-09-25 09:29:51.405  INFO 12532 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2018-09-25 09:29:51.726  INFO 12532 --- [       runner-0] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@66dfc8e0: startup date [Tue Sep 25 09:29:51 CST 2018]; root of context hierarchy
2018-09-25 09:29:53.881  INFO 12532 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-09-25 09:29:53.944  INFO 12532 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-09-25 09:29:53.946  INFO 12532 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-09-25 09:29:53.969  INFO 12532 --- [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: [C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\PuTTY\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\WINDOWS\System32\OpenSSH\;D:\wamp\bin\php\php7.0.29;C:\Program Files (x86)\nodejs\;D:\software\spring-2.0.5.RELEASE\bin;D:\Program Files\Python3\Scripts\;D:\Program Files\Python3\;C:\Users\hema\AppData\Local\Microsoft\WindowsApps;C:\Users\hema\AppData\Roaming\npm;.]
2018-09-25 09:29:54.187  INFO 12532 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader  : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@76960b9d class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2018-09-25 09:29:54.288  INFO 12532 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-09-25 09:29:54.289  INFO 12532 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2563 ms
2018-09-25 09:29:54.443  INFO 12532 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-09-25 09:29:54.452  INFO 12532 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-25 09:29:54.452  INFO 12532 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-25 09:29:54.452  INFO 12532 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-25 09:29:54.454  INFO 12532 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-25 09:29:54.629  INFO 12532 --- [       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-09-25 09:29:54.847  INFO 12532 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@66dfc8e0: startup date [Tue Sep 25 09:29:51 CST 2018]; root of context hierarchy
2018-09-25 09:29:54.970  INFO 12532 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String FirstApplication.welcome()
2018-09-25 09:29:54.975  INFO 12532 --- [       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-09-25 09:29:54.978  INFO 12532 --- [       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-09-25 09:29:55.026  INFO 12532 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-25 09:29:55.028  INFO 12532 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-25 09:29:55.894  INFO 12532 --- [       runner-0] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-09-25 09:29:56.022  INFO 12532 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-25 09:29:56.033  INFO 12532 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 5.404 seconds (JVM running for 64.879)
2018-09-25 09:38:27.210  INFO 12532 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-09-25 09:38:27.211  INFO 12532 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-09-25 09:38:27.242  INFO 12532 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 29 ms

第4步:在浏览器中浏览应用程序

上面基于Spring应用现已准备就绪。打开网址为http:// localhost:8080/ ,将看到以下输出 -

理解关键执行过程

以下操作由Spring CLI执行 -

  • 所有依赖项JAR仅在第一次使用时下载。
  • Spring CLI根据代码中使用的类和注释自动检测要下载的依赖项JAR。
  • 最后,它编译代码,在嵌入式tomcat上部署war,在默认端口8080上启动嵌入式tomcat服务器。

上一篇:Spring Boot CLI环境安装

下一篇:grab依赖扣除

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程