史上最簡單的SpringBoot教程之如何列印HelloWorld
一、前言
Spring boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。
Spring boot包含的特性:
- 建立獨立的Spring應用程式
- 嵌入Tomcat,無需部署war檔案
- 簡化Maven配置
- 自動配置Spring
- 提供生產就緒功能,如指標,健康檢查和外部配置
- 開箱即用,沒有程式碼生成,也無需XML配置
為基於Spring的開發提供更快的入門體驗,開箱即用,沒有程式碼生成,也無需XML配置。同時也可以修改預設值來滿足特定需求。提供了一些大型專案中常見的非功能特性,如嵌入式伺服器、安全、指標、健康體檢、外部配置等。Spring boot並不是對Spring功能的增強,而是提供了一種快速使用Spring的方式。
二、準備
開發環境:1.7.0_79
開發工具:Eclipse
專案管理工具:Maven3
作業系統:Windows8
Spring boot版本:1.4.1.RELEASE
三、案例
搭建一個Maven工程SpringBootHelloWorld
POM檔案中匯入相關依賴
我們引入了Spring boot的父節點依賴,引入這個之後相關的引用就不需要新增version配置了,Spring boot會自動選擇最合適的版本進行新增。<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.queen.springboot</groupId> <artifactId>SpringBootHelloWorld</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
環境中我們指定JDK的版本為1.7,預設是1.6
匯入spring-boot-starter-web依賴這個地方我們沒有指定版本號,是因為上面我們配置了parent,所以不用添加了。
編寫控制類SpringBootHelloWorld
@RestController public class HelloWorldController { @RequestMapping("/hello") public String sayHello() { return "helloWorld"; } }
這裡我們使用的@RestController註解等價於@Controller和@ResponseBody註解,使用@RequestMapping建立請求對映。
編寫啟動類APP
@SpringBootApplication
public class APP {
public static void main(String[] args) {
SpringApplication.run(APP.class, args);
}
}
使用@SpringBootApplication註解定義APP是一個SpringBoot型別的應用,在main方法中使用SpringApplication的run方法啟動我們的程式。
右鍵Run As -> Java Application,啟動應用,控制檯啟動日誌如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.1.RELEASE)
2017-07-16 10:31:07.706 INFO 12108 --- [ main] com.queen.controller.APP : Starting APP on Lenovo-PC with PID 12108 (E:\queen\SpringBootHelloWorld\target\classes started by think in E:\queen\SpringBootHelloWorld)
2017-07-16 10:31:07.709 INFO 12108 --- [ main] com.queen.controller.APP : No active profile set, falling back to default profiles: default
2017-07-16 10:31:07.814 INFO 12108 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]6568614: startup date [Sun Jul 16 10:31:07 CST 2017]; root of context hierarchy
2017-07-16 10:31:10.086 INFO 12108 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-16 10:31:10.099 INFO 12108 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-07-16 10:31:10.101 INFO 12108 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5
2017-07-16 10:31:10.201 INFO 12108 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-16 10:31:10.202 INFO 12108 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2392 ms
2017-07-16 10:31:10.411 INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-16 10:31:10.415 INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-16 10:31:10.417 INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-16 10:31:10.417 INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-16 10:31:10.417 INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-16 10:31:10.812 INFO 12108 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]6568614: startup date [Sun Jul 16 10:31:07 CST 2017]; root of context hierarchy
2017-07-16 10:31:10.906 INFO 12108 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.queen.controller.HelloWorldController.sayHello()
2017-07-16 10:31:10.913 INFO 12108 --- [ 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-07-16 10:31:10.913 INFO 12108 --- [ 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-07-16 10:31:10.965 INFO 12108 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-16 10:31:10.966 INFO 12108 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-16 10:31:11.013 INFO 12108 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-16 10:31:11.320 INFO 12108 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-16 10:31:11.391 INFO 12108 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-16 10:31:11.398 INFO 12108 --- [ main] com.queen.controller.APP : Started APP in 4.301 seconds (JVM running for 4.619)
啟動後預設埠號是8080♦使用http://localhost:8080/hello看是否能夠訪問我們的應用
訪問成功。
至此,我們使用Spring boot搭建的第一個HelloWorld小程式完成。
=======歡迎大家拍磚,小手一抖,多多點贊喲!=======