springboot初探——启动流程

编程


其他的我们可以先不管,先来看一眼springboot的main方法

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

这里做了什么?一个注解,一个SpringApplication的静态方法,这两步完成了springboot项目启动的所有步骤。

现在我们来研究到底做了些什么。

注解先不去管它,顺着SpringApplication#run这个方法往下走

public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {

return run(new Class<?>[] { primarySource }, args);

}

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {

return new SpringApplication(primarySources).run(args);

}

可以看到这两个静态方法,实际上做的事又是实例化SpringApplication对象,并调用run方法。所以这时候我们还可以把启动类写成这样

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

// SpringApplication.run(DemoApplication.class, args);

SpringApplication springApplication = new SpringApplication(DemoApplication.class);

springApplication.run(args);

}

}

我们可以先拿到一个SpringApplication的实例,然后再通过实例调用run方法,继续看下边的操作。

public SpringApplication(Class<?>... primarySources) {

this(null, primarySources);

}

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

}

这里可以看到,在实例化SpringApplication对象时,做了好多操作:

  1. 设置resourceLoader,加载资源用的,我们可以自己实现,加载外部资源等
  2. primarySources设置bean的主要加载来源
  3. webApplicationType确定启动的方式,WebApplicationType.deduceFromClasspath()这个方法要注意一下,WebApplicationType是个枚举类,通过判断当前路径下有没有某个类来确定启动方式的,目前支持三种方式(NONE,SERVLET,REACTIVE)

	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;

}

  1. setInitializers((Collection)getSpringFactoriesInstances(ApplicationContextInitializer.class))是加载spring启动需的上下文
  2. 设置监听
  3. 设置启动主类

    这里有一个4/5/6步有一个技术点,稍后再详细说

经过这几步以后,就加载完启动一个sping应用所需的资源!

接下来就是run方法了,先贴上代码,乍一看,好长的代码,这都是干了啥啊。

	/**

* 运行Spring应用程序,创建并刷新一个新的ApplicationContext。

* @param args args应用程序参数(通常从Java主方法传递)

* @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;

}

未完待续

以上是 springboot初探——启动流程 的全部内容, 来源链接: utcz.com/z/512878.html

回到顶部