1.1 原有Spring優缺點分析
1.1.1 Spring的優點分析
1.1.2 Spring的缺點分析
1.2 SpringBoot的概述
1.2.1 SpringBoot解決上述Spring的缺點
1.2.2 SpringBoot的特點
1.2.3 SpringBoot的核心功能
二、SpringBoot快速入門
2.1 代碼實現
2.1.1 創建Maven工程
2.1.2 添加SpringBoot的起步依賴
SpringBoot要求,項目要繼承SpringBoot的起步依賴spring-boot-starter-parent
<!-- 所有的SpringBoot工程都必須繼承 spring-boot-starter-parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent>
SpringBoot要集成SpringMVC進行Controller的開發,所以項目要導入web的啓動依賴
<dependencies> <!-- web功能起步依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--熱部署配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
2.1.3 編寫SpringBoot引導類
要通過SpringBoot提供的引導類起步SpringBoot纔可以進行訪問
package xyz.ytfs; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author by 雨聽風說 * @Classname MySpringBootApplication * @Description TODO(SpringBoot的引導類) * @Date 2020/5/12 1:27 */ //申明該類是一個springboot的啓動類 @SpringBootApplication public class MySpringBootApplication { /** * main 是java程序的入口 * @param args */ public static void main(String[] args) { //run方法 表示運行springBoot的引導類 run參數就是springBoot引導類的字節碼對象 SpringApplication.run(MySpringBootApplication.class); } }
2.1.4 編寫Controller
在引導類MySpringBootApplication同級包或者子級包中創建QuickStartController
/** * @author by 雨聽風說 * @Classname QuickController * @Description TODO(快速入門的controller) * @Date 2020/5/12 1:32 */ @Controller public class QuickController { @RequestMapping("quick") @ResponseBody public String quickStart(){ return "hellow world"; } }
2.1.5 測試
執行SpringBoot起步類的主方法,控制檯打印日誌如下
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-05-12 21:00:02.770 INFO 10340 --- [ restartedMain] xyz.ytfs.MySpringBootApplication : Starting MySpringBootApplication on 一一 with PID 10340 (started by DTX in F:\project folder\winter vacation\springBoot)
2020-05-12 21:00:02.773 INFO 10340 --- [ restartedMain] xyz.ytfs.MySpringBootApplication : No active profile set, falling back to default profiles: default
2020-05-12 21:00:02.918 INFO 10340 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-05-12 21:00:02.918 INFO 10340 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-05-12 21:00:04.144 INFO 10340 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-12 21:00:04.164 INFO 10340 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-12 21:00:04.164 INFO 10340 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-05-12 21:00:04.242 INFO 10340 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-12 21:00:04.242 INFO 10340 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1323 ms
2020-05-12 21:00:04.421 INFO 10340 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-12 21:00:04.587 INFO 10340 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2020-05-12 21:00:04.624 INFO 10340 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-12 21:00:04.627 INFO 10340 --- [ restartedMain] xyz.ytfs.MySpringBootApplication : Started MySpringBootApplication in 2.304 seconds (JVM running for 4.797)
通過日誌發現,Tomcat started on port(s): 8080 (http) with context path ''
tomcat已經起步,端口監聽8080,web應用的虛擬工程名稱爲空
打開瀏覽器訪問url地址爲:http://localhost:8080/quick