Spring boot入門一 環境搭建HelloWorld
說在前面:
最近Spring boot也火了一把,不稍微熟習下Spring boot都不好意思出門了,於是我也試著看下Spring boot的相關配置。
概述:
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。一句話概括,“spring boot它就是一些人為了快速配置一套系統框架而進行的封裝,裡面根據各種型別的專案功能封裝了各種模組框架的引用,廣大老百姓在使用的時候就不需要一一的去新增具體非常細的引用了,只需要新增他們封裝好的spring boot相關模組外掛即可”
配置工具:
1、JDK1.7
2、Maven(也可以使用非maven工具搭建)
3、IDE(Eclipse、IntelliJ 或者其它的)
程式碼配置:
1、新建一個maven專案,可以是web工程也可以是java基本工程
2、pom.xml中配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3、新建個java類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }
好了以上配置完成,已經完成了spring boot最最簡單的配置,
執行該main方法
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.0.RELEASE)
2017-08-30 16:05:10.511 INFO 5180 --- [ main] c.s.project.controller.SampleController : Starting SampleController on lenovo2017 with PID 5180 (E:\Eclipse4_WorkPlace\spring_boot\spring_boot_first\target\classes started by lenovo in E:\Eclipse4_WorkPlace\spring_boot\spring_boot_first)
2017-08-30 16:05:10.512 INFO 5180 --- [ main] c.s.project.controller.SampleController : No active profile set, falling back to default profiles: default
2017-08-30 16:05:10.540 INFO 5180 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]1b4e829: startup date [Wed Aug 30 16:05:10 CST 2017]; root of context hierarchy
2017-08-30 16:05:11.371 INFO 5180 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-30 16:05:11.378 INFO 5180 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-08-30 16:05:11.379 INFO 5180 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.4
2017-08-30 16:05:11.441 INFO 5180 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/aaa] : Initializing Spring embedded WebApplicationContext
2017-08-30 16:05:11.441 INFO 5180 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 902 ms
2017-08-30 16:05:11.554 INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-30 16:05:11.557 INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-30 16:05:11.557 INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-30 16:05:11.558 INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-30 16:05:11.558 INFO 5180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-08-30 16:05:12.066 INFO 5180 --- [ost-startStop-1] o.a.c.util.SessionIdGeneratorBase : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [495] milliseconds.
2017-08-30 16:05:12.301 INFO 5180 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]1b4e829: startup date [Wed Aug 30 16:05:10 CST 2017]; root of context hierarchy
2017-08-30 16:05:12.346 INFO 5180 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.sam.project.controller.SampleController.home()
2017-08-30 16:05:12.351 INFO 5180 --- [ 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-08-30 16:05:12.352 INFO 5180 --- [ 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-08-30 16:05:12.370 INFO 5180 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-30 16:05:12.370 INFO 5180 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-30 16:05:12.397 INFO 5180 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-30 16:05:12.486 INFO 5180 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-08-30 16:05:12.530 INFO 5180 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-30 16:05:12.532 INFO 5180 --- [ main] c.s.project.controller.SampleController : Started SampleController in 2.398 seconds (JVM running for 2.642)
可以看到上面列印了各種資訊,表示啟動成功!
瀏覽器訪問
http://localhost:8080/
即可看到輸出hello world資訊!
相關說明:
@EnableAutoConfiguration 和 SpringApplication 。
1、@EnableAutoConfiguration 用於自動配置。它會根據你的pom配置(實際上應該是根據具體的依賴)來判斷這是一個什麼應用,並建立相應的環境。
在上面這個例子中,@EnableAutoConfiguration 會判斷出這是一個web應用,所以會建立相應的web環境。
2、SpringApplication 則是用於從main方法啟動Spring應用的類。預設,它會執行以下步驟:
- 建立一個合適的ApplicationContext例項 (取決於classpath)。
- 註冊一個CommandLinePropertySource,以便將命令列引數作為Spring properties。
- 重新整理application context,載入所有單例beans。
- 啟用所有CommandLineRunner beans。
至此,一個入門簡單的spring boot建立完成。