玩转springboot:入门程序
Spring Boot 入門
一、Spring Boot 簡介
官網英文:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
官網中文說法:
Spring引導使您可以輕松地創建獨立的、生產級的基于Spring的應用程序,您可以“直接運行”。
我們對Spring平臺和第三方庫采取了一種固執己見的觀點,這樣您就可以開始使用最小的忙亂。大多數Spring啟動應用程序都需要非常少的Spring配置。
二、環境準備
- jdk1.8:Spring Boot 推薦jdk1.7及以上;
- maven3.x:maven 3.3以上版本;
- IntelliJIDEA2018:IntelliJ IDEA 2018.1.4
- SpringBoot 1.5.9.RELEASE:1.5.9
三、Spring Boot HelloWorld程序
瀏覽器發送hello請求,服務器接受請求并處理,響應Hello World字符串;
1、創建一個maven工程;
2、導入spring boot相關的依賴
<parent><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐starter‐parent</artifactId><version>1.5.9.RELEASE</version> </parent> <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐starter‐web</artifactId></dependency> </dependencies>3、編寫一個主程序;啟動Spring Boot應用
/*** @author 歐陽思海* @date 2018/7/25 9:41*/ @SpringBootApplication public class HelloWorldMainApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(HelloWorldMainApplication.class, args);} }4、編寫相關的Controller
/*** @author 歐陽思海* @date 2018/7/25 9:42*/ @Controller public class HelloController {@ResponseBody@RequestMapping("/hello")public String hello() {return "Hello World!";} }5、運行主程序測試
四、Hello World深入理解
1、POM文件
- 父項目
這個還有一個父項目:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐dependencies</artifactId><version>1.5.9.RELEASE</version><relativePath>../../spring‐boot‐dependencies</relativePath> </parent>**Spring Boot的版本仲裁中心:**他來真正管理Spring Boot應用里面的所有依賴版本,以后我們導入依賴默認是不需要寫版本(沒有在dependencies里面管理的依賴自然需要聲明版本號)。
- 啟動器
spring-boot-starter:spring-boot場景啟動器,幫我們導入了web模塊正常運行所依賴的組件。
Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啟動器),只需要在項目里面引入這些starter
相關場景的所有依賴都會導入進來。要用什么功能就導入什么場景的啟動器
2、主程序類,主入口類
package com.sihai.springboot_test_quick;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class SpringbootTestQuickApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTestQuickApplication.class, args);} }@SpringBootApplication: Spring Boot應用標注在某個類上說明這個類是SpringBoot的主配置類,SpringBoot
就應該運行這個類的main方法來啟動SpringBoot應用。
然后,我們再看看其源代碼:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class} ), @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {@AliasFor(annotation = EnableAutoConfiguration.class)Class<?>[] exclude() default {};@AliasFor(annotation = EnableAutoConfiguration.class)String[] excludeName() default {};@AliasFor(annotation = ComponentScan.class,attribute = "basePackages")String[] scanBasePackages() default {};@AliasFor(annotation = ComponentScan.class,attribute = "basePackageClasses")Class<?>[] scanBasePackageClasses() default {}; }@SpringBootConfiguration:Spring Boot的配置類。
標注在某個類上,表示這是一個Spring Boot的配置類。
@Configuration:配置類上來標注這個注解;
配置類 ----- 配置文件;配置類也是容器中的一個組件;@Component
@EnableAutoConfiguration:開啟自動配置功能;
以前我們需要配置的東西,Spring Boot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啟自
動配置功能;這樣自動配置才能生效;
@AutoConfigurationPackage:自動配置包
@Import(AutoConfigurationPackages.Registrar.class):
Spring的底層注解@Import,給容器中導入一個組件;導入的組件由AutoConfigurationPackages.Registrar.class;
將主配置類(@SpringBootApplication標注的類)的所在包及下面所有子包里面的所有組件掃描到Spring容器;
EnableAutoConfigurationImportSelector:導入哪些組件的選擇器。
將所有需要導入的組件以全類名的方式返回;這些組件就會被添加到容器中;
會給容器中導入非常多的自動配置類(xxxAutoConfiguration);就是給容器中導入這個場景需要的所有組件,
并配置好這些組件。
五、使用Spring Initializer快速創建Spring Boot項目
1、點擊idea的maven創建項目
2、按照maven的步驟就行
3、創建controller
/*** @author 歐陽思海* @date 2018/7/25 9:57*/ @Controller public class HelloworldController {@ResponseBody@RequestMapping("/hello")public String hello(){return "Hello World!";} }4、運行主程序SpringbootTestQuickApplication
這就是springboot的入門程序了。
總結
以上是生活随笔為你收集整理的玩转springboot:入门程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bat等大公司常考java多线程面试题
- 下一篇: 玩转springboot:配置文件详细讲