zoukankan      html  css  js  c++  java
  • 1.springboot启动流程

    SpringBoot版本:2.1.2.RELEASE

    1.maven

     <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.2.RELEASE</version>
     </parent>
    
       <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    

    2.主程序入口,两种方式

    •   SpringApplication.run(Application.class);
    •   new SpringApplication(Application.class).run(args);可以通过设置springApplication的defaultProperties等属性,设置一些配置参数信息

      2.1. 初始化springApplication对象

        

        listeners.starting();    EventPublishingRunListener

        发布一个ApplicationStartingEvent事件

      •  LoggingApplicationListener
      •  BackgroundPreinitializer
      •    DelegatingApplicationListener
      •    LiquibaseServiceLocatorApplicationListener
    
    
    
    

      2.2. 准备环境变量 Environment

      2.2.1.org.springframework.boot.SpringApplication#prepareEnvironment

           org.springframework.boot.SpringApplication#getOrCreateEnvironment         

    new StandardServletEnvironment() ->new StandardEnvironment() ->new AbstractEnvironment()

    public AbstractEnvironment() { this.propertyResolver = new PropertySourcesPropertyResolver(this.propertySources); this.customizePropertySources(this.propertySources); } #StandardServletEnvironment protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new StubPropertySource("servletConfigInitParams")); propertySources.addLast(new StubPropertySource("servletContextInitParams")); if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { propertySources.addLast(new JndiPropertySource("jndiProperties")); } super.customizePropertySources(propertySources); } #StandardEnvironment protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new MapPropertySource("systemProperties", this.getSystemProperties())); propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", this.getSystemEnvironment())); }

     此时,environment中的propertySources包含servletConfigInitParams,servletContextInitParams,jndiProperties(存在的话,spring.properties中配置spring.jndi.ignore=true,且...待定),systemProperties,systemEnvironment

          org.springframework.boot.SpringApplication#configureEnvironment

     protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
            MutablePropertySources sources = environment.getPropertySources();
            if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
                sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
            }
    
            if (this.addCommandLineProperties && args.length > 0) {
                String name = "commandLineArgs";
                if (sources.contains(name)) {
                    PropertySource<?> source = sources.get(name);
                    CompositePropertySource composite = new CompositePropertySource(name);
                    composite.addPropertySource(new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
                    composite.addPropertySource(source);
                    sources.replace(name, composite);
                } else {
                    sources.addFirst(new SimpleCommandLinePropertySource(args));
                }
            }
    
        }
    

          org.springframework.boot.SpringApplicationRunListeners#environmentPrepared

          发布一个ApplicationEnvironmentPreparedEvent

       

    1. ConfigServerBootstrapApplicationListener
      private PropertySource<?> propertySource = new MapPropertySource("configServerClient", Collections.singletonMap("spring.cloud.config.enabled", "false"));
      
      public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
              ConfigurableEnvironment environment = event.getEnvironment();
              if (!environment.resolvePlaceholders("${spring.cloud.config.enabled:false}").equalsIgnoreCase("true") && !environment.getPropertySources().contains(this.propertySource.getName())) {
                  environment.getPropertySources().addLast(this.propertySource);
              }
      
          }
      
    2.  BootstrapApplicationListener
    3.  LoggingSystemShutdownListener
    4.  ConfigFileApplicationListener
    5.  AnsiOutputApplicationListener
    6.  LoggingApplicationListener
    7.  BackgroundPreinitializer
    8.  ClasspathLoggingApplicationListener
    9.  DelegatingApplicationListener
    10.  FileEncodingApplicationListener

      

      2.3. 初始化ApplicatonContext

         org.springframework.boot.SpringApplication#createApplicationContext

         创建一个AnnotationConfigApplicationContext

         org.springframework.boot.SpringApplication#prepareContext

         发布一个ApplicationContextInitializedEvent

         org.springframework.boot.SpringApplication#refreshContext

            发布一个ContextRefreshedContext,初始化所有的Bean

     

     

       

  • 相关阅读:
    利用Java API通过路径过滤上传多文件至HDFS
    docker-compose部署haproxy
    PM2 常用命令
    shell脚本循环访问url直到状态码返回200跳出循环
    CentOS 7.6安装配置Chrony同步系统时钟
    PostgreSQL9.5数据库锁表问题分析与解决
    linux系统netstat命令详解(netstat常用运维命令)
    linux目录备份脚本
    PostgreSQL 锁等待监控 珍藏级SQL
    docker部署pgadmin4并通过nginx反向代理
  • 原文地址:https://www.cnblogs.com/Hleaves/p/10348779.html
Copyright © 2011-2022 走看看