Springboot 項目搭建入門
環境準備ideamaven工程搭建springboot項目由於其自動配置了很多的依賴,簡化了開發者的配置,因此加快了開發者的開發速度,但是如果對spring 底層等不太了解的人,還是有些懵的,建議大家學習spring 之後再來使用spring boot項目來搭建。搭建很簡單<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-01</artifactId>
<dependencies>
<!--添加web依賴,版本由父工程來決定-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
編寫Controller業務類package com.shadow.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class SpringbootController { @RequestMapping("/hello") @ResponseBody public String hello(){ return "hello"; }}編寫spring boot啟動類package com.shadow;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;//@SpringBootApplication 來標註一個主程序類//說明這是一個Spring Boot應用@SpringBootApplicationpublic class SpringbootApplication { public static void main(String[] args) { //以為是啟動了一個方法,沒想到啟動了一個服務 SpringApplication.run(SpringbootApplication.class, args);}}引入springboot依賴包 注意Controller包一定要和SpringbootApplication啟動類平級,不然掃描不到。
結果
本文使用SpringBoot搭建了一個web項目,可以發現相對於ssm來搭建web項目,springboot確實幫我們做了很多的工作。
springboot-boot-starter-xx:就是spring-boot的場景啟動器我們需要什麼場景,在pom中引入對應的啟動器即可,例如spring-boot-starter-web:幫我們導入了web模塊正常運行所依賴的組件;
2. 自動配置核心是在@EnableAutoConfiguration這個註解
springboot核心流程分析
此代碼SpringApplication.run(SpringbootApplication.class, args);執行流程
構造函數中流程//推斷項目類型,看項目是不是web項目this.webApplicationType = WebApplicationType.deduceFromClasspath();//加載所有實現了ApplicationContextInitializer類的初始化器setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));//加載所有實現了ApplicationListener類的程序監聽器setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//推斷main方法所在的類,裡面使用異常堆棧來判斷的this.mainApplicationClass = deduceMainApplicationClass();run方法的流程//啟動應用程式啟動計時器StopWatchstopWatch=newStopWatch();stopWatch.start();ConfigurableApplicationContextcontext=null;Collection<SpringBootExceptionReporter>exceptionReporters=newArrayList<>();//配置系統屬性configureHeadlessProperty();//獲取所有的監聽器,並啟動SpringApplicationRunListenerslisteners=getRunListeners(args);listeners.starting();try {//裝配環境參數ApplicationArgumentsapplicationArguments=newDefaultApplicationArguments(args);ConfigurableEnvironmentenvironment=prepareEnvironment(listeners, applicationArguments);configureIgnoreBeanInfo(environment);//列印bannerBannerprintedBanner=printBanner(environment);//創建上下文環境context=createApplicationContext();//準備上下文異常報告器exceptionReporters=getSpringFactoriesInstances(SpringBootExceptionReporter.class,newClass[] { ConfigurableApplicationContext.class }, context);//上下文前置處理,environment環境設置,監聽配置prepareContext(context, environment, listeners, applicationArguments, printedBanner);//刷新上下文,此處是執行spring的bean實例化過程refreshContext(context);//上下文後置處理afterRefresh(context, applicationArguments);//計時器結束stopWatch.stop();if (this.logStartupInfo) {newStartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); }//通知應用上下文啟動完成listeners.started(context);//執行callRunnerscallRunners(context, applicationArguments);}//通知應用上下文就緒listeners.running(context); returncontext;