Springboot快速入门
2017-11-10 本文已影响12人
it_zzy
Springboot快速入门
简介
在您第1次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用Spring框架的时候,是否觉得一堆反复黏贴的配置有一些厌烦?那么您就不妨来试试使用Spring Boot来让你更易上手,更简单快捷地构建Spring应用!
Spring Boot让我们的Spring应用变的更轻量化。比如:你可以仅仅依靠一个Java类来运行一个Spring引用。你也可以打包你的应用为jar并通过使用java -jar来运行你的Spring Web应用。
Spring Boot的主要优点:
- 为所有Spring开发者更快的入门
- 开箱机用,提供各种默认配置来简化项目配置
- 内嵌式容器,简化web项目
- 没有冗余代码生成和XML配置的要求
快速入门
本章主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。
系统要求
Java 7及以上
Spring Framework 4.1.5及以上
本文采用Java 1.8.0_73、Spring Boot 1.5.8调试通过。
使用Maven构建项目
- 1.通过SPRING INITIALIZR工具产生基础项目
- 1.访问:http://start.spring.io/
- 2.选择构建工具Maven Project、Spring Boot版本1.3.2以及一些工程基本信息,可参考下图所示SPRING INITIALIZR
SPRING INITIALIZR - 3.点击Generate Project下载项目压缩包
- 2.解压项目包,并用IDE以Maven项目导入,以IntelliJ IDEA 14为例:
- 1.菜单中选择File–>New–>Project from Existing Sources...
- 2.选择解压后的项目文件夹,点击OK
- 3.点击Import project from external model并选择Maven,点击Next到底为止。
- 4.若你的环境有多个版本的JDK,注意到选择Java SDK的时候请选择Java 7以上的版本
项目结构解析
[图片上传失败...(image-e4796c-1510299088962)]
通过上面步骤完成了基础项目的创建,如上图所示,SpringBoot的基础结构共三个文件(具体路径根据用户生成项目时填写的Group所有差异):
- src/main/java下的程序入口:SpringbootdemoApplication
- src/main/resources下的配置文件:application.properties
- src/test/下的测试入口:SpringbootdemoApplicationTests
生成的SpringbootdemoApplication和SpringbootdemoApplicationTests类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。
引入Web模块
当前的pom.xml内容如下,仅引入了两个模块:
- spring-boot-starter:核心模块,包括自动配置支持、日志和YAML
- spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
引入Web模块Web模块,需添加spring-boot-starter-web模块:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
编写Hello服务
- 创建HelloController类,内容如下:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String test(){
return "hello springboot";
}
}
- 启动主程序,输入部分日志如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.8.RELEASE)
2017-11-10 14:41:25.956 INFO 5186 --- [ main] c.z.s.d.s.SpringbootdemoApplication : Starting SpringbootdemoApplication on zzydeMBP with PID 5186 (/Users/zzy/Documents/zzy/Idea_workspace/springboot_learn/springbootdemo/target/classes started by zzy in /Users/zzy/Documents/zzy/Idea_workspace/springboot_learn/springbootdemo1)
2017-11-10 14:41:25.961 INFO 5186 --- [ main] c.z.s.d.s.SpringbootdemoApplication : No active profile set, falling back to default profiles: default
2017-11-10 14:41:26.028 INFO 5186 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7ff2a664: startup date [Fri Nov 10 14:41:26 CST 2017]; root of context hierarchy
2017-11-10 14:41:27.138 INFO 5186 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-11-10 14:41:27.149 INFO 5186 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-11-10 14:41:27.150 INFO 5186 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23
2017-11-10 14:41:27.230 INFO 5186 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-11-10 14:41:27.230 INFO 5186 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1206 ms
2017-11-10 14:41:27.314 INFO 5186 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-11-10 14:41:27.317 INFO 5186 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-11-10 14:41:27.317 INFO 5186 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-11-10 14:41:27.318 INFO 5186 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-11-10 14:41:27.318 INFO 5186 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-11-10 14:41:27.564 INFO 5186 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7ff2a664: startup date [Fri Nov 10 14:41:26 CST 2017]; root of context hierarchy
2017-11-10 14:41:27.602 INFO 5186 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.zzy.springboot.demo.springbootdemo.controller.HelloController.test()
2017-11-10 14:41:27.605 INFO 5186 --- [ main] 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.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-11-10 14:41:27.605 INFO 5186 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-11-10 14:41:27.622 INFO 5186 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-10 14:41:27.622 INFO 5186 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-10 14:41:27.642 INFO 5186 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-10 14:41:27.729 INFO 5186 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-11-10 14:41:27.775 INFO 5186 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-11-10 14:41:27.781 INFO 5186 --- [ main] c.z.s.d.s.SpringbootdemoApplication : Started SpringbootdemoApplication in 2.131 seconds (JVM running for 3.001)
2017-11-10 14:42:09.029 INFO 5186 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-11-10 14:42:09.029 INFO 5186 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-11-10 14:42:09.045 INFO 5186 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
- 打开浏览器访问http://localhost:8080/hello,可以看到页面输出hello springboot
[图片上传失败...(image-8c79ef-1510299088962)]