Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作,Spring Boot让我们的Spring应用变的更轻量化。
Spring Boot的主要优点:
为所有Spring开发者更快的入门
开箱即用,提供各种默认配置来简化项目配置
内嵌式容器简化Web项目
没有冗余代码生成和XML配置的要求
SpringBoot 2 基于Spring5和JDK8
本例jdk:8u73,eclipse:oxygen
创建初始化文件
下载到本地后,eclipse maven项目导入
src/main/java下的程序入口:Chapter1Application
src/main/resources下的配置文件:application.properties
src/test/下的测试入口:Chapter1ApplicationTests
引入Web模块
在当前的pom.xml中,包含
spring-boot-starter:核心模块,包括自动配置支持、日志等
spring-boot-starter-test:测试模块,包括JUnit等
新增web依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
创建hello world Controller
特别注意,一定要在com.xx.bootDmo1子包下
HelloController
package com.xx.bootDmo1.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String print() { return "Hi Spring Boot2.0.2"; } }
启动执行BootDmo1Application
在java文件BootDmo1Application中右键Debug As -> Java Application
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.2.RELEASE) 2018-05-31 16:46:14.347 INFO 8444 --- [ main] com.xx.bootDmo1.BootDmo1Application : Starting BootDmo1Application on WIN-N71UG691JA6 with PID 8444 (C:\Users\Administrator\Desktop\bootDmo1\bootDmo1\target\classes started by Administrator in C:\Users\Administrator\Desktop\bootDmo1\bootDmo1) 2018-05-31 16:46:14.354 INFO 8444 --- [ main] com.xx.bootDmo1.BootDmo1Application : No active profile set, falling back to default profiles: default 2018-05-31 16:46:14.474 INFO 8444 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@54bff557: startup date [Thu May 31 16:46:14 CST 2018]; root of context hierarchy 2018-05-31 16:46:17.116 INFO 8444 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-05-31 16:46:17.203 INFO 8444 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-05-31 16:46:17.203 INFO 8444 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31 2018-05-31 16:46:17.230 INFO 8444 --- [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\Java\jdk1.8.0_73\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Java\jdk1.8.0_73\jre\bin;C:/Program Files/Java/jre1.8.0_73/bin/server;C:/Program Files/Java/jre1.8.0_73/bin;C:/Program Files/Java/jre1.8.0_73/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8.0_73\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;E:\install\pl\PLSQL\instantclient_11_2;E:\install\apache-maven-3.3.3\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;E:\install\Git\cmd;E:\install\svn-c\bin;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\Program Files\MySQL\MySQL Server 5.7\bin;E:\install\apache-maven-3.1.1\bin;E:\install\putty\;E:\install\ue\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;E:\install\eclise_oxygen\eclipse;;.] 2018-05-31 16:46:17.401 INFO 8444 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-05-31 16:46:17.402 INFO 8444 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2932 ms 2018-05-31 16:46:17.718 INFO 8444 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-05-31 16:46:17.726 INFO 8444 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-05-31 16:46:17.727 INFO 8444 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-05-31 16:46:17.727 INFO 8444 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-05-31 16:46:17.727 INFO 8444 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-05-31 16:46:18.075 INFO 8444 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-05-31 16:46:18.639 INFO 8444 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@54bff557: startup date [Thu May 31 16:46:14 CST 2018]; root of context hierarchy 2018-05-31 16:46:18.798 INFO 8444 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.xx.bootDmo1.controller.HelloController.print() 2018-05-31 16:46:18.808 INFO 8444 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-05-31 16:46:18.809 INFO 8444 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-05-31 16:46:18.887 INFO 8444 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-05-31 16:46:18.887 INFO 8444 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-05-31 16:46:19.241 INFO 8444 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-05-31 16:46:19.388 INFO 8444 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-05-31 16:46:19.405 INFO 8444 --- [ main] com.xx.bootDmo1.BootDmo1Application : Started BootDmo1Application in 5.844 seconds (JVM running for 6.918)
浏览器访问