前面已经介绍一下springboot,本篇开始介绍springboot在启动过程中做了什么,凭什么那么少的代码就能完成一个web项目。
其他的我们可以先不管,先来看一眼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对象时,做了好多操作:
- 设置resourceLoader,加载资源用的,我们可以自己实现,加载外部资源等
- primarySources设置bean的主要加载来源
- 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;
}
- setInitializers((Collection)getSpringFactoriesInstances(ApplicationContextInitializer.class))是加载spring启动需的上下文
- 设置监听
- 设置启动主类
这里有一个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();
//1. 加载所有能加载到的监听器
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
// 2. 加载启动时写在命令行上的参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 3. 准备环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
// 4. 打印banner
Banner printedBanner = printBanner(environment);
// 5. 创建上下文,这里用到了之前设置的项目类型
context = createApplicationContext();
// 6. 获取异常报告事件监听
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
// 7. 准备上下文,调用了监听器的contextPrepared和contextLoaded
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 8. 调用refresh方法,用过spring的都知道,这里开始扫描并且初始化bean
refreshContext(context);
// 9. 这里是个空方法
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
// 10. 调用监听器started的事件
listeners.started(context);
// 11. 加载并调用runners
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
// 12. 调用监听器的running方法
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
ok,run方法每一步的操作都做了什么都以注释的方式写在上边的代码里了。 未完待续
来源:oschina
链接:https://my.oschina.net/whitegarnett/blog/3160023