Springboot是什麼

springboot是什麼

個人認爲Springboot是 Spring的升級優化,SpringBoot省去一些經常重複的一樣的框架搭建配置工作,提高開發效率,本質是Spring + 其他框架。

Springboot之前,很常見SSM和SSH的框架系統,本人在工作中初期系統也是這些框架搭建

SSM一般指SpringMVC+Spring+mybatis,
SSH 一般指Structs+Spring+Hibernate

開發中系統可能會有其他擴展,如數據庫連接,mysql,oracle,連接池配置,非關係型數據庫,MongoDB,Redis,RPC連接服務調用dubbo ,消息隊列 activemq,kafka等,這些都需要整合配置,很多時候可能十個系統業務不同,九個系統的架構都是SSM,核心一直都是spring,所以開發做多了很容有想到做一個架子模板,搭建十個系統都用它,只是業務不同,搭建好架子後主要是寫業務代碼;
springboot至少有這樣的理由出現,提高開發效率,並且可以根據業務不同配置各種個性化。
原先一個SpringMVC + Spring + Mybatis的雛形框架搭建可能要一個小時搭好,有了SpringBoot快的話幾分鐘能搭好,並且內置Tomcat,可以啓動服務。

springboot可以很方便的"一鍵開發部署",他整合了很多框架,提高開發效率,所以spring boot主要做的就是將各種功能組件整合,並且有默認配置;一般使用maven一步就能構建springboot項目

pom.xml 基本配置 maven3.6.1

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.12.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.domoment</groupId>
	<artifactId>httpconcurrent</artifactId>
	<version>0.0.1</version>
	<name>httpconcurrent</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-json</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

springboot項目只需要一個註解和main方法就可以啓動

@SpringBootApplication //一個註解
public class TesApplication {

	public static void main(String[] args) { //main方法啓動
        SpringApplication.run(TesApplication.class, args);
	}
}

這裏運行main方法就可以啓動一個springboot項目,並且啓動了tomcat服務器(默認),原理需要從org.springframework.boot.SpringApplication入手

其他

SpringApplication

org.springframework.boot.SpringApplication是springboot項目的關鍵啓動類

SpringApplication在運行main方法的時候其實做了很多,包括讀取配置文件,校驗,加載類,反射創建對象,初始化等,看看下面代碼,SpringApplication.run()方法做了什麼

//SpringApplication.run(TesApplication.class, args); -->
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
		return new SpringApplication(primarySources).run(args);
	}

總表面看是創建了一個SpringApplication對象,然後調用對象的run方法

構造方法

// -->
// 構造方法
@SuppressWarnings({ "unchecked", "rawtypes" })
	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}

構造方法主要是做一些基本配置,spring.factories的讀取,類文件的加載校驗,基本初始化類初始化,監聽器的初始化,容器類型的指定

非靜態run方法

// SpringApplication對象的run方法 非靜態
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        configureIgnoreBeanInfo(environment);
        Banner printedBanner = printBanner(environment);
        context = createApplicationContext();
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                                                         new Class[] { ConfigurableApplicationContext.class }, context);
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        refreshContext(context);
        afterRefresh(context, applicationArguments);
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        listeners.started(context);
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }

    try {
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    return context;
}

可以看到,run方法代碼內容還是挺多的,通過字面理解,這裏基本是大的方向的定義,
從參數讀取->配置環境讀取->忽略bean對象信息讀取->banner圖片輸出->創建applicationContext->
......
->refreshContext 啓動容器 ->
.....listenner.running(context)啓動後的一些配置

tomcat在refreshContext中創建啓動

一般springboot的tomcat創建啓動是在refreshContext方法中被執行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章