springboot那点事

spring boot

Springboot主要提供用户快速搭建WEB应用,简化配置文件,mevan默认引入包解决版本冲突问题,内嵌tomcat快启动web

Springboot工程结构,POM.xml,使用mevan管理包的版本依赖管理。

  • 引用springboot父工程

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.8.RELEASE</version>
</parent>

  • spring-boot-starter-parent里又引用默认的依赖集
<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-dependencies</artifactId>

    <version>2.2.8.RELEASE</version>

    <relativePath>../../spring-boot-dependencies</relativePath>

  </parent>
  • spring-boot-dependencies 默认引用的所有包版本。

    <activemq.version>5.15.12</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.80</appengine-sdk.version>
    <artemis.version>2.10.1</artemis.version>
    <aspectj.version>1.9.5</aspectj.version>
    <assertj.version>3.13.2</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
    <awaitility.version>4.0.3</awaitility.version>
    <bitronix.version>2.1.4</bitronix.version>
    <build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
    <byte-buddy.version>1.10.11</byte-buddy.version>
    <caffeine.version>2.8.4</caffeine.version>
    <cassandra-driver.version>3.7.2</cassandra-driver.version>
    

启动过程

SpringApplication.run(Application.class, args);//启动应用入口

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();//ApplicationEvent 调用监听器触发ApplicationEvent事件,典型的监听者设计模式

        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            
            //创建servlet上下文AnnotationConfigServletWebServerApplicationContext
            context = this.createApplicationContext();							
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);//启动Spring上下文的refresh加载容器
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            listeners.started(context);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

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

appliction 启动时可以通过这些事件进行拦截处理

hdk
class org.springframework.boot.context.event.ApplicationStartingEvent
hdk
class org.springframework.boot.context.event.ApplicationStartingEvent
hdk
class org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
hdk
class org.springframework.boot.context.event.ApplicationContextInitializedEvent
hdk
class org.springframework.boot.context.event.ApplicationPreparedEvent
hdk
class org.springframework.context.event.ContextRefreshedEvent
hdk
class org.springframework.boot.context.event.ApplicationPreparedEvent
hdk
class org.springframework.boot.context.event.ApplicationStartedEvent

tomcat启动

ServletWebServerApplicationContext

    protected void onRefresh() {
        super.onRefresh();

        try {
            this.createWebServer();//创建tomcat
        } catch (Throwable var2) {
            throw new ApplicationContextException("Unable to start web server", var2);
        }
    }

    protected void finishRefresh() {
        super.finishRefresh();
        WebServer webServer = this.startWebServer();
        if (webServer != null) {
            this.publishEvent(new ServletWebServerInitializedEvent(webServer, this));
        }

    }

启动自动配置spring.boot.autoconfigure,此包里有各种包的配置类,简化了之前繁琐的xml配置,

注解@SpringBootApplication-》@EnableAutoConfiguration作用主要是告诉SpringFactoriesLoader

去 META-INF/spring.factories下加载org.springframework.boot.autoconfigure.EnableAutoConfiguration的所有配置。

总结

springboot快速构建WEB应用,减少各种插件的配置,包的版本依赖管理,使用autoconfigure提供默认的配置。

posted on 2021-03-16 14:15  wolf12  阅读(62)  评论(0编辑  收藏  举报