一个spring boot项目的启动过程分析

1、web.xml 定义入口类

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.baosight.ApplicationBoot</param-value>
</context-param>

2、主入口类: ApplicationBoot,SpringBoot项目的mian函数


@SpringBootApplication(scanBasePackages = "com.bs")
@ServletComponentScan("com.bs.4j.core.web.servlet")
@ImportResource(locations = {"classpath*:spring/framework/platApplicationContext*.xml", "classpath*:spring/framework/applicationContext*.xml"})
@EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
public class ApplicationBoot extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ApplicationBoot.class);
        app.run(args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ApplicationBoot.class);
    }

}

3、SpringApplication

SpringApplication类是Spring Boot框架的核心组件之一,它负责整个应用的启动和初始化过程。SpringApplication的设计目标是简化Spring应用的启动步骤,提供一套开箱即用的配置,让开发者能够快速启动和运行Spring应用。

在这个类中:

3.1、SpringApplication构造方法

/**
	 * Create a new {@link SpringApplication} instance. The application context will load
	 * beans from the specified primary sources (see {@link SpringApplication class-level}
	 * documentation for details. The instance can be customized before calling
	 * {@link #run(String...)}.
	 * @param primarySources the primary bean sources
	 * @see #run(Class, String[])
	 * @see #SpringApplication(ResourceLoader, Class...)
	 * @see #setSources(Set)
	 */
	public SpringApplication(Class<?>... primarySources) {
		this(null, primarySources);
	}

	/**
	 * Create a new {@link SpringApplication} instance. The application context will load
	 * beans from the specified primary sources (see {@link SpringApplication class-level}
	 * documentation for details. The instance can be customized before calling
	 * {@link #run(String...)}.
	 * @param resourceLoader the resource loader to use
	 * @param primarySources the primary bean sources
	 * @see #run(Class, String[])
	 * @see #setSources(Set)
	 */
	@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();
	}

 对于构造函数的解读

  1. 参数初始化

    • resourceLoader:这是一个资源加载器,用于从类路径或其他位置加载资源,如配置文件、静态资源等。
    • primarySources:这是应用程序的主要配置源,通常是一个或多个配置类的数组,用于引导Spring的自动配置和组件扫描。
  2. 非空检查

    • primarySources进行非空检查,确保至少有一个配置源被提供,否则将抛出IllegalArgumentException
  3. 配置源存储

    • primarySources转换为LinkedHashSet集合并存储,保持插入顺序,这有助于后续的配置解析和组件扫描。
  4. Web应用类型推断

    • 调用WebApplicationType.deduceFromClasspath()方法自动检测应用的Web类型,是Servlet-based、Reactive-based还是None(非Web应用)。这一决策影响了Spring Boot如何初始化Web环境。
  5. ApplicationContext Initializers设置

    • 调用getSpringFactoriesInstances方法来获取所有实现了ApplicationContextInitializer接口的实例,并将其设置为SpringApplication的initializers。这些initializers会在ApplicationContext创建过程中被调用,用于自定义上下文的初始化。
  6. Application Listeners设置

    • 同样地,通过getSpringFactoriesInstances方法获取所有实现了ApplicationListener接口的实例,并将其设置为SpringApplication的listeners。这些listeners会在应用生命周期的不同阶段被触发,如启动、停止等。
  7. 主应用类推断

    • 调用deduceMainApplicationClass方法来确定应用的主类,通常是启动应用的那个类,用于在某些情况下(如生成文档)标识应用本身。

 3.1.1、WebApplicationType.deduceFromClasspath()

检测应用的Web类型,在下文ConfigurableApplicationContext的实例化中会使用到

 3.1.2、setInitializers

初始化classpath下 META-INF/spring.factories中已配置的ApplicationContextInitializer

	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
		return getSpringFactoriesInstances(type, new Class<?>[] {});
	}

	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type,
			Class<?>[] parameterTypes, Object... args) {
		ClassLoader classLoader = getClassLoader();
		// Use names and ensure unique to protect against duplicates
		Set<String> names = new LinkedHashSet<>(
				SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
				classLoader, args, names);
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}

1、getSpringFactoriesInstances有什么用,做了什么,这个方法是根据给定的类型typeClassLoader,从spring-core提供的FACTORIES_RESOURCE_LOCATION=META-INF/spring.factories文件中加载所有该类型的工厂类名,即获取指定的类(key)的同一入口方法。这里使用了LinkedHashSet来存储这些名字,确保它们是唯一的且按插入顺序排序。这一块在SpringFactoriesLoader.loadFactoryNames/loadSpringFactories中实现

public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
		String factoryClassName = factoryClass.getName();
		return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
	}

	private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
		MultiValueMap<String, String> result = cache.get(classLoader);
		if (result != null) {
			return result;
		}

		try {
			Enumeration<URL> urls = (classLoader != null ?
					classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
			result = new LinkedMultiValueMap<>();
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				UrlResource resource = new UrlResource(url);
				Properties properties = PropertiesLoaderUtils.loadProperties(resource);
				for (Map.Entry<?, ?> entry : properties.entrySet()) {
					String factoryClassName = ((String) entry.getKey()).trim();
					for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
						result.add(factoryClassName, factoryName.trim());
					}
				}
			}
			cache.put(classLoader, result);
			return result;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load factories from location [" +
					FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
	}

在这里,获取的是key为 org.springframework.context.ApplicationContextInitializer 的类。ApplicationContextInitializer 是Spring框架的类, 这个类的主要目的就是在   ConfigurableApplicationContext 调用refresh()方法之前,回调这个类的initialize方法。通过  ConfigurableApplicationContext 的实例获取容器的环境Environment,从而实现对配置文件的修改完善等工作。

 

3.1.3 、setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

 ApplicationListener 的加载过程和上面的 ApplicationContextInitializer 类的加载过程是一样的.ApplicationListener 是spring的事件监听器,典型的观察者模式,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现对spring容器全生命周期的监听

SpringApplication 类的实例化过程,我们可以在spring容器创建之前做一些预备工作,和定制化的需求。比如,自定义SpringBoot的Banner,比如自定义事件监听器,再比如在容器refresh之前通过自定义 ApplicationContextInitializer 修改配置一些配置或者获取指定的bean都是可以的

3.2、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方法详解:

  • 计时和异常处理设置

    • 创建一个StopWatch对象来测量应用启动所需的时间。

    • 初始化一个空列表exceptionReporters,用于存放异常报告器。

  • 配置无头模式

    • 调用configureHeadlessProperty方法确保应用在没有图形界面的环境中也能运行。

  • 监听器初始化

    • 获取SpringApplicationRunListeners监听器实例,并调用starting方法通知监听器应用即将开始启动。

  • 解析命令行参数

    • 创建ApplicationArguments实例,用于解析传入的命令行参数。

  • 环境准备

    • 准备ConfigurableEnvironment环境,这个环境包含了应用的配置信息。应用上下文环境包括什么呢?包括计算机的环境,Java环境,Spring的运行环境,Spring项目的配置(在SpringBoot中就是那个熟悉的application.properties/yml)等等。

  • 忽略Bean信息配置

    • 调用configureIgnoreBeanInfo方法,这通常用于性能优化,避免加载不必要的Bean信息。

  • 打印Banner

    • 打印应用的启动Banner,显示应用名、版本等信息。

  • 创建ApplicationContext

    • 创建ConfigurableApplicationContext上下文,这是Spring管理Bean的容器。

  • 异常报告器获取

    • 通过getSpringFactoriesInstances方法获取所有SpringBootExceptionReporter实例。

  • 上下文准备

    • 调用prepareContext方法进行上下文的准备工作,包括注册Bean后处理器、监听器等。

  • 刷新上下文

    • 调用refreshContext方法刷新上下文,这是Spring Bean生命周期的关键点。

  • 启动后操作

    • 调用afterRefresh方法执行一些应用启动后的操作,如初始化定时任务等。

  • 日志记录

    • 如果配置了记录启动信息,使用StartupInfoLogger记录应用启动完成的日志。

  • 监听器回调

    • 通知监听器应用已经启动。

  • 运行Runners

    • 调用callRunners方法执行定义好的ApplicationRunnerCommandLineRunner,它们通常用于执行一些启动后的初始化工作。

  • 异常处理

    • 在整个启动过程中捕获并处理可能发生的异常,确保应用能够优雅地处理错误。

  • 返回ApplicationContext

    • 最终返回创建的ConfigurableApplicationContext实例,这是Spring Boot应用的核心组件。

3.3、ConfigurableApplicationContext

ConfigurableApplicationContext是Spring框架中ApplicationContext接口的一个扩展,它提供了更多的配置和控制能力,是Spring应用程序上下文的高级接口。在Spring Boot中,ConfigurableApplicationContext扮演着核心角色,用于管理应用程序的生命周期和Bean的配置。下面是一些关键特性及其作用的详细解读:

 配置和定制

ConfigurableApplicationContext允许开发者在应用程序启动时对上下文进行更深入的定制,例如设置EnvironmentResourceLoaderMessageSourceApplicationEventPublisher等。这使得你可以更灵活地调整Spring容器的行为,以适应不同的应用场景。

 刷新上下文

ApplicationContext不同的是,ConfigurableApplicationContext提供了refresh()方法,用于手动刷新上下文,重新加载配置并初始化Bean。这是非常有用的,特别是在测试场景中,你需要在每次测试之前重置Spring上下文的状态。

关闭上下文

ConfigurableApplicationContext提供了close()方法,允许你优雅地关闭上下文,释放资源。这对于那些不需要一直运行的应用程序尤其重要,比如批处理作业或命令行应用。

监听器支持

它支持ApplicationListener,允许你在上下文的生命周期事件(如启动、关闭、Bean实例化等)中注册监听器,以便于执行自定义逻辑。这对于集成日志记录、健康检查等功能非常有用。

环境感知

ConfigurableApplicationContext允许你访问和修改应用的Environment,这包括读取和设置配置属性、切换不同的配置文件(profiles)等。这对于多环境部署(如开发、测试、生产)的管理至关重要。

事件发布

除了标准的ApplicationEventPublisher功能,ConfigurableApplicationContext还提供了更细粒度的事件发布和监听机制,这对于实现复杂的业务逻辑和系统集成非常有帮助。

实例化

在Spring Boot中,SpringApplicationrun方法通过createApplicationContext返回一个ConfigurableApplicationContext实例,这通常是应用程序的主要入口点,可以通过这个上下文访问到所有由Spring管理的Bean和资源。下面则是createApplicationContext的具体实现:

Strategy method used to create the ApplicationContext. By default this method will respect any explicitly set application context or application context class before falling back to a suitable default.
返回值:
the application context (not yet refreshed)
请参阅:
setApplicationContextClass(Class)
protected ConfigurableApplicationContext createApplicationContext() {
		Class<?> contextClass = this.applicationContextClass;
		if (contextClass == null) {
			try {
				switch (this.webApplicationType) {
				case SERVLET:
					contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
					break;
				case REACTIVE:
					contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
					break;
				default:
					contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
				}
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException(
						"Unable create a default ApplicationContext, "
								+ "please specify an ApplicationContextClass",
						ex);
			}
		}
		return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
	}

3.2.1、WebApplicationType详解 

WebApplicationType,一个枚举,且里面定义了三个枚举值,分别是 NONE、SERVLET、REACTIVE

SERVLET:传统的Servlet-based web应用,内嵌基于 servlet 的 web 服务器(如:Tomcat,Jetty,Undertow 等,其实现在大多Java网站应用都是采用的基于 Tomcat 的 servlet 类型服务器),默认使用AnnotationConfigServletWebServerApplicationContext作为上下文。

REACTIVE:响应式web应用,在 servlet 容器当中,采用命令式编程方式,代码一句一句的执行,这样更有利于理解与调试,而在反应式的 web 编程中,是基于异步响应式,现在 WebFlux 框架就是一个较为流行的反应式编程,默认使用AnnotationConfigReactiveWebServerApplicationContext作为上下文。

NONE:既不是SERVLET也不是REACTIVE:即非Web应用,非 web 应用程序(不内嵌服务器),则默认使用AnnotationConfigApplicationContext作为上下文。

而关于Springboot 如何确定程序的应用类型 WebApplicationType 的?

在SpringApplication构造方法:

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();
	}

this.webApplicationType = WebApplicationType.deduceFromClasspath();其中 WebApplicationType.deduceFromClasspath() 这个方法实现如下

	static WebApplicationType deduceFromClasspath() {
		if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)
				&& !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
				&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
			return WebApplicationType.REACTIVE;
		}
		for (String className : SERVLET_INDICATOR_CLASSES) {
			if (!ClassUtils.isPresent(className, null)) {
				return WebApplicationType.NONE;
			}
		}
		return WebApplicationType.SERVLET;
	}

ClassUtils.isPresent()?干什么的?用来判断一个类的存在性的,当这个类存在,那么返回的就是 true;当这个类不存在时,返回的结果就是 fasle。

(1)WEBFLUX_INDICATOR_CLASS,WEBMVC_INDICATOR_CLASS,JERSEY_INDICATOR_CLASS常量值又分别是什么?看源码

private static final String WEBMVC_INDICATOR_CLASS = "org.springframework."
			+ "web.servlet.DispatcherServlet";

	private static final String WEBFLUX_INDICATOR_CLASS = "org."
			+ "springframework.web.reactive.DispatcherHandler";

	private static final String JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer";

当项目中存在 DispatcherHandler 这个类,且不存在 DispatcherServlet 类和ServletContainer时,程序的应用类型就是 REACTIVE,也就是他会加载嵌入一个反应式的 web 服务器。

(2)而SERVLET_INDICATOR_CLASSES

private static final String[] SERVLET_INDICATOR_CLASSES = { "javax.servlet.Servlet",
			"org.springframework.web.context.ConfigurableWebApplicationContext" };

当项目中 Servlet 和 ConfigurableWebApplicationContext 其中一个不存在时,则程序的应用类型为 NONE,它并不会加载内嵌任何 web 服务器。

(3)除了上面两种情况外,其余的都按 SERVLET 类型处理,会内嵌一个 servlet 类型的 web 服务器

上面的这些类的判定,都来源于 Spring 的相关依赖包,而这依赖包是否需要导入,也是开发者所决定的,所以说开发者可以决定程序的应用类型,并不是 Srpingboot 本身决定的。

 3.4、refreshContext

  • 29
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值