开源项目

知识点

相关文章

更多

最近更新

更多

Spring Boot CLI快速入门程序

2019-04-29 21:59|来源: 网路

第1步 - 安装Spring Boot CLI

可以从 https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/ 下载最新版本的Spring Boot CLI API的ZIP文件存档。 下载安装后,将zip分发包装到方便的位置。 例如,在Windows上的D:\software\spring-2.0.5.RELEASE\bin或Linux/Unix上的/usr/local/spring-2.0.5.RELEASE中。

确保在此目录中正确设置CLASSPATH变量,否则在运行应用程序时将遇到问题。
或者暂时在命令提示符中设置路径以运行spring boot应用程序,如下所示 -

D:/software > set path=D:\software\spring-2.0.5.RELEASE\bin;%PATH%Shell

第2步 - 验证安装

在命令提示符下运行以下命令以验证安装 -

D:\software>spring --versionShell

它应打印以下输出,确认安装成功 -

Spring CLI v2.0.5.RELEASE



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

第1步:创建源文件夹

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

第2步:创建源文件

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

@RestController
class FirstApplication {
   @RequestMapping("/")
   String welcome() {
      "Welcome to 656463.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.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
  • 该类已被删除,并由org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory取代。更多信息请参见: Spring引导M1发行说明 The class has been removed and replaced by org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory For more info check: Spring- ...
  • Web技术与桌面技术不同。 计算机是分开的:您的Web浏览器位于您的计算机上,并且该网页位于服务器上。 单击按钮时,Web浏览器会创建TCP / IP“Internet”数据包并将其发送到服务器,以便Web应用程序可以决定如何处理它。 在Java / AWT桌面应用程序中,使用ActionListeners,单击按钮最终会调用正在计算机上运行的应用程序。 即使它可以通过“事件模式”实现这样做,它仍然只是在您的应用程序中调用该方法。 在Spring Framework中,应用程序使用Controller处理“ ...
  • 我最近从Groovy项目启动了Spring Context ,它允许您使用repl groovy交互式控制台访问Remote Shell插件中的所有bean对象。 看起来像这样: $ ssh -p 2000 user@localhost user@localhost's password: . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ ...
  • 不需要从pom.xml删除安全性。 在你的项目中,你可以尝试下面的东西。 尝试创建SecurityConfig ,它将扩展WebSecurityConfigurerAdapter并提供一些用户名和密码,之后您可以对其进行自定义。 import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.Au ...
  • 您必须在application.properties文件中为jsp文件定义前缀和后缀,如下所示: spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jsp You have to define the prefix and suffix for you jsp file in application.properties file like following: spring.mvc.view.prefix: /WEB-INF/j ...
  • 在现有的Spring应用程序中引入Spring Boot是非常可行的,但可能取决于您的Spring版本。 我已经应用了几次的成功方法是。 将依赖项升级到Spring Boot支持的依赖项 让Spring Boot Starter项目管理您的依赖项 创建一个Spring Boot启动类 剥离配置以使用Spring Boot自动配置 升级依赖项 如果您有不同的依赖关系,那么在直接升级到Spring Boot时,Spring Boot可能会突然出现更新的,不兼容的依赖关系。 我倾向于做的是,一次升级依赖项1并确保 ...
  • 我遇到了同样的问题,因此我必须查看此页面上的所有解决方案以及相关的解决方案。 没有人对我有好处。 这就是我进行小规模研究的原因,看起来捕获的TCP端口出现问题只是因为Gradle和mvn都不了解TCP端口的子操作。 所以不要杀死进程只需使用命令: $ gradlew –stop (我希望mvn同样存在) 此命令正常关闭由Gradle启动的守护程序并释放由Tomcat端口捕获的守护程序。 I have met the same issue and as result I had to review all ...
  • Spring启动基于构建工具打包配置创建jar / war。 如果你提到包装类型为war,那么spring boot将创建.war文件。 使用以下配置,然后您将获得.war 。 Maven的 war 摇篮 apply plugin: 'war' Spring boot creates the jar/war based of the build tool packaging configuration. If you mention the packagin ...