- Spring Boot简介
- Spring Boot快速入门
- Spring Boot引导过程
- Spring Boot Tomcat部署
- Spring Boot构建系统
- Spring Boot代码结构
- Spring Boot Bean和依赖注入
- Spring Boot运行器(Runner)
- Spring Boot应用程序属性
- Spring Boot日志
- Spring Boot构建RESTful Web服务
- Spring Boot异常处理
- Spring Boot拦截器
- Spring Boot Servlet过滤器
- Spring Boot Tomcat端口号
- Spring Boot Rest模板
- Spring Boot文件处理
- Spring Boot服务组件
- Spring Boot Thymeleaf示例
- Spring Boot使用RESTful Web服务
- Spring Boot CORS支持
- Spring Boot国际化
- Spring Boot调度
- Spring Boot启用HTTPS
- Spring Boot Eureka服务器
- Spring Boost Eureka服务注册
- Spring Boot Zuul代理服务器和路由
- Spring Boot云配置服务器
- Spring Boot云配置客户端
- Spring Boot Actuator
- Spring Boot管理服务器
- Spring Boot管理客户端
- Spring Boot启用Swagger2
- Spring Boot创建Docker镜像
- Spring Boot跟踪微服务日志
- Spring Boot Flyway数据库
- Spring Boot发送电子邮件
- Spring Boot Hystrix
- Spring Boot Web Socket
- Spring Boot批量服务
- Spring Boot Apache Kafka
- Spring Boot单元测试用例
- Spring Boot Rest控制器单元测试
- Spring Boot数据库源(连接数据库)
- Spring Boot保护Web应用程序
Spring Boot快速入门
本章将演示如何使用Maven
和Gradle创建Spring Boot应用程序。
前提条件
系统需要具有以下创建Spring Boot应用程序的最低要求 -
- Java 7
- Maven 3.2
- Gradle 2.5
Spring Boot CLI
Spring Boot CLI是一个命令行工具,它用于运行Groovy脚本。它是使用Spring Boot命令行界面创建Spring Boot应用程序的最简单方法。可以在命令提示符下创建,运行和测试应用程序。
本节介绍手动安装Spring Boot CLI所涉及的步骤。 如需进一步的帮助,可以使用以下链接:
还可以从Spring Software存储库下载Spring CLI发行版:
对于手动安装,需要使用以下两个文件夹 -
- spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.zip
- spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.tar.gz
下载后,解压缩归档文件并按照install.txt
文件中给出的步骤进行操作。 并不是说它不需要任何环境设置。
在Windows中,转到命令提示符下的Spring Boot CLI bin目录并运行命令spring --version
以确保正确安装了spring CLI。 执行命令后,可以看到spring CLI版本,如下所示 -
使用Groovy运行Hello World
创建一个包含Rest Endpoint
脚本的简单groovy文件:hello.groovy,并使用spring boot CLI
运行groovy文件。代码如下所示 -
@Controller class Example { @RequestMapping("/") @ResponseBody public String hello() { "Hello Spring Boot" } }
请注意,在这个示例中,已经配置Spring Boot CLI bin目录到系统环境变量,并把文件:hello.groovy
保存到D:/worksp/springboot/
目录。使用命令spring run hello.groovy
运行应用程序,如下面所示 -
D:\worksp\springboot>spring run hello.groovy
执行结果如下所示:
D:\worksp\springboot>spring run hello.groovy . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.5.RELEASE) 2018-09-26 09:34:13.782 INFO 13908 --- [ runner-0] o.s.boot.SpringApplication : Starting application on DESKTOP-CAN8JLM with PID 13908 (started by hema in D:\worksp\springboot) 2018-09-26 09:34:13.810 INFO 13908 --- [ runner-0] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default 2018-09-26 09:34:14.747 INFO 13908 --- [ runner-0] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ec39a62: startup date [Wed Sep 26 09:34:14 CST 2018]; root of context hierarchy 2018-09-26 09:34:18.655 INFO 13908 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-09-26 09:34:18.735 INFO 13908 --- [ runner-0] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-09-26 09:34:18.736 INFO 13908 --- [ runner-0] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34 2018-09-26 09:34:18.756 INFO 13908 --- [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-26 09:34:19.030 INFO 13908 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@24ef272d class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader 2018-09-26 09:34:19.140 INFO 13908 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-09-26 09:34:19.140 INFO 13908 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4398 ms 2018-09-26 09:34:19.303 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-09-26 09:34:19.311 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-09-26 09:34:19.318 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-09-26 09:34:19.320 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-09-26 09:34:19.322 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-09-26 09:34:19.556 INFO 13908 --- [ 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-26 09:34:19.831 INFO 13908 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ec39a62: startup date [Wed Sep 26 09:34:14 CST 2018]; root of context hierarchy 2018-09-26 09:34:19.998 INFO 13908 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String Example.hello() 2018-09-26 09:34:20.004 INFO 13908 --- [ 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-26 09:34:20.006 INFO 13908 --- [ 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-26 09:34:20.050 INFO 13908 --- [ 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-26 09:34:20.053 INFO 13908 --- [ runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-26 09:34:20.651 INFO 13908 --- [ runner-0] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-09-26 09:34:20.821 INFO 13908 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-09-26 09:34:20.830 INFO 13908 --- [ runner-0] o.s.boot.SpringApplication : Started application in 9.092 seconds (JVM running for 15.353)
运行groovy文件,所需的依赖项将自动下载,它将在Tomcat 8080端口启动应用程序,如下面给出的屏幕截图所示 -
Tomcat启动后,转到Web浏览器并点击URL => http://localhost:8080/
,可以看到输出,如图所示:
上一篇:Spring Boot简介
下一篇:Spring Boot引导过程
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
扫描二维码
程序员编程王