spring boot 启动

spring boot 启动的说明

在静态语句块中,加载了

servlet 环境 来源 命名类 SERVLET_ENVIRONMENT_SOURCE_NAMES jndiProperties servletContextInitParams servletConfigInitParams

deduceWebEnvironment-判断java web环境,是

javax.servlet.Servlet即非mvc环境,或者是org.springframework.web.context.ConfigurableWebApplicationContext,即spring的web环境

setInitializers 初始化属性

获得实现ApplicationContextInitializer

  1. SpringFactoriesLoader.loadFactoryNames 在所有的项目中查找factory查找中找到META-INF/spring.factories获得factories。 属性名为:org.springframework.context.ApplicationContextInitializer

在org.springframework.boot:spring-boot中找到

org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer, org.springframework.boot.context.ContextIdApplicationContextInitializer, org.springframework.boot.context.config.DelegatingApplicationContextInitializer, org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer

在org.springframework.boot:spring-boot-autoconfigure中

org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer

  1. createSpringFactoriesInstances 创建spring工厂实体
  2. 排序

配置监听

ApplicationListener(10个)

在org.springframework.boot:spring-boot中找到

org.springframework.boot.ClearCachesApplicationListener, org.springframework.boot.builder.ParentContextCloserApplicationListener, org.springframework.boot.context.FileEncodingApplicationListener, org.springframework.boot.context.config.AnsiOutputApplicationListener, org.springframework.boot.context.config.ConfigFileApplicationListener, org.springframework.boot.context.config.DelegatingApplicationListener, org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener, org.springframework.boot.logging.ClasspathLoggingApplicationListener, org.springframework.boot.logging.LoggingApplicationListener

在org.springframework.boot:spring-boot-autoconfigure中 org.springframework.boot.autoconfigure.BackgroundPreinitializer

获得实例,实例排序

### 推断主引用类 deduceMainApplicationClass

获得当前运行的堆栈类的方法运行,这样获得main方法的类 设置mainApplicationClass类在SpringApplication

启动 java.awt.headless模式

java.awt.headless

一般是在程序开始**headless模式,告诉程序,现在你要工作在Headless mode下,就不要指望硬件帮忙了,你得自力更生,依靠系统的计算能力模拟出这些特性来

springApplication运行时的监听者

SpringApplicationRunListener

在org.springframework.boot:spring-boot中找到

org.springframework.boot.context.event.EventPublishingRunListener

在org.springframework.boot:spring-boot-autoconfigure中

没有找到

启动监听

启动org.springframework.boot.context.event.EventPublishingRunListener

执行 multicastEvent

ApplicationEvent 所有的程序事件类继承ApplicationEvent,这个类是抽象的类,不会对已经直接发布的一般事件起作用。

SimpleApplicationEventMulticaster 对ApplicationEventMulticaster接口的简单实现。

对于所有登记的监听者,使用多路所有的事件(?翻译错误了),让监听者忽略那些他们不感兴趣的事件。 监听者通常会对传入的事件对象进行相应的校验。

作为默认的,这个调用线程会唤醒所有的监听者。 这个允许在整个程序阻塞那些有危险的监听者,但是添加最小的花销。特别是,一个可供选择的任务执行这需要监听者们在不同的线程(比如从线程池中)执行。

ResolvableType (来源于spring4的新特性)

封装一个java反射类型,提供访问父类的类型,获得接口,和获得通用参数。随着最终分解为一个类的能力。

这是一个更加简单易用的泛型操作支持。

forInstance ,创建一个实现ResolvableTypeProvider的特殊的类

org.springframework.boot.context.event.ApplicationStartedEvent[[email protected]]

ListenerCacheKey

在SimpleApplicationEventMulticaster中缓冲map中存放监听者。

AbstractApplicationEventMulticaster.retrieveApplicationListeners

listener是GenericApplicationListener的类型,就返回GenericApplicationListener,如果不是,返回GenericApplicationListenerAdapter

判断是否支持事件类型,

eventType是ResolvableType,即ApplicationStartedEvent是ResolvableType,同时ApplicationEvent不为空

判断监听者是否支持 源类型即SpringApplication 监听不是SmartApplicationListener子类,为true。

可以的监听为: ConfigFileApplicationListener LoggingApplicationListener DelegatingApplicationListener LiquibaseServiceLocatorApplicationListener

invokeListener

ConfigurableEnvironment

大多数不是全部都是Environment types实现这个这个配置接口。提供工具,该工具可以设置活跃或者默认的profiles,也可以操作基本的属性来源。允许客户设置和验证所需的属性,定制转化服务。更多的通过ConfigurablePropertyResolver超接口

PropertySourcesPropertyResolver getProperty(“spring.profiles.active”)属性

重新走AbstractApplicationEventMulticaster方法,只不过这回 eventType成了 org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent

在准备环境中有7个可用监听

ConfigFileApplicationListener AnsiOutputApplicationListener LoggingApplicationListener ClasspathLoggingApplicationListener BackgroundPreinitializer DelegatingApplicationListener FileEncodingApplicationListener

springBoot 的启动

spring boot 启动

创建一个SpringApplication实例,

spring boot 启动

spring boot 启动 往SpringApplication中的sources添加当前的的启动类

deduceWebEnvironment:决定web环境

spring boot 启动 默认的两种环境: private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet", "org.springframework.web.context.ConfigurableWebApplicationContext" };

spring boot 启动 判定 需要的类是否存在,并可以可以被加载,返回

spring boot 启动spring boot 启动

判断profiles级别

配置初始化

spring boot 启动spring boot 启动

找到main方法

spring boot 启动

spring boot 启动

准备一个干净的context等待刷新

告诉 父类图刷新网络BeanFactory

springBoot

springboot的加载过程:

  1. 启动一个进程
  2. 是否启用spring切面
  3. 刷新注解
  4. 注册bean给jmx 在启动之后曝光
  5. 开启所有的beans
  6. 服务启动

如果是使用了web-Starter

  1. 启动一个线程
  2. 是否启用spring切面
  3. 自动嵌入注解配置
    • tomcat启动
    • 启动tomcat
    • tomcat的servlet引擎启动
    • 初始化 spring嵌入WebApplicationContext
      • WebApplicationContext初始化完成
      • 在servlet注册类中注册servlet-使用默认的servlet
      • 在filter注册类中 * 注册filter-使用metricFilter * 注册filter-使用 characterEncodingFilter * 注册filter-使用 hiddenHttpMethodFilter * 注册filter-使用 httpPutFormContentFilter * 注册filter-使用 requestContextFilter * 注册filter-使用 webRequestLoggingFilter * 注册filter-使用 applicationContextIdFilter
      • @ControllerAdvice 查找控制器增强器
      • 在Controller中匹配
        • BasicErrorController 错误页面 Mapped "{[/error]}",Mapped "{[/error],produces=[text/html]}"
        • ResourceHttpRequestHandler Mapped URL path [/webjars/],Mapped URL path [/],Mapped URL path [/**/favicon.ico]
        • HealthMvcEndpoint Mapped "{[/health || /health.json],produces=[application/json]}"
        • EndpointMvcAdapter
          • Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/json]}",
          • Mapped "{[/info || /info.json],methods=[GET],produces=[application/json]}" ,
          • Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/json]}" ,
          • {[/dump || /dump.json],methods=[GET],produces=[application/json]}" ,
          • {[/configprops || /configprops.json],methods=[GET],produces=[application/json]},
          • {[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/json],
          • {[/beans || /beans.json],methods=[GET],produces=[application/json]}
          • {[/env/{name:.*}],methods=[GET],produces=[application/json]}
          • {[/env || /env.json],methods=[GET],produces=[application/json]}
          • {[/trace || /trace.json],methods=[GET],produces=[application/json]}
        • MetricsMvcEndpoint Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/json]}",
        • HeapdumpMvcEndpoint {[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]},
  4. 登记beans到JMX中
  5. tomcat启动成功
  6. 服务启动成功。共耗时为多长

spring boot 启动

jar包启动,是在org.springframework.boot.loader.JarLauncher中启动的。

程序启动是在自己配置的main方法启动的

转载于:https://my.oschina.net/dylw/blog/994707