zoukankan      html  css  js  c++  java
  • 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提供默认的配置。

  • 相关阅读:
    cuda、cudnn环境配置
    1.python的 a,b=b,a+b 和 a=b b=a+b 的区别
    例子:循环语句--打印一个菱形
    7.python基础语法--format()函数
    例子:循环语句--九九乘法表
    例子:循环语句--输入N个整数,判断最大值,最小值,和,平均值。
    例子:循环语句--素数问题
    例子:循环语句--打印一个边长为n的正方形。
    例子:循环语句--给定一个不超过5位的正整数,判断该数的位数,依次打印出个位、十位、百位、千位、万位的数字。
    6.python基础语法--循环结构
  • 原文地址:https://www.cnblogs.com/wolf12/p/14543224.html
Copyright © 2011-2022 走看看