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 Yiibai.Com"
   }
}

第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.ser[email protected]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.c[email protected]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.ser[email protected]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 Boot CLI入門程序

理解關鍵執行過程

以下操作由Spring CLI執行 -

  • 所有依賴項JAR僅在第一次使用時下載。
  • Spring CLI根據代碼中使用的類和註釋自動檢測要下載的依賴項JAR。
  • 最後,它編譯代碼,在嵌入式tomcat上部署war,在默認端口8080上啓動嵌入式tomcat服務器。