springboot应用启动过程分析(基于springboot 1.5.4)

示例

springboot程序通过SpringBootApplication注解和Runner类中入口方法main执行SpringApplication.run()就可以执行起来,如下:

1
2
3
4
5
6
7
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

内部代码分析

通过DemoApplication main方法进入之后,过掉各种中间多态或抽象的过程,主要包括两个方法: initializerrun

在启动过程中的各种Spring context和bean的配置,主要在spring-boot的 META-INF/spring.factories文件中

META-INF中这个文件也是开发springboot插件的一个方式

initializer方法

1
2
3
4
5
6
7
8
9
10
11
@SuppressWarnings({ "unchecked", "rawtypes" })
private void initialize(Object[] sources) {
if (sources != null && sources.length > 0) {
this.sources.addAll(Arrays.asList(sources));
}
this.webEnvironment = deduceWebEnvironment();
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}

主要包含如下几个过程:

  1. 判断是否为web环境
  2. 设置初始化类(initializers, springboot 封装的 spring-context中的ApplicationContext列表)
  3. 设置监听器(listeners, spring封装的spring-context中的ApplicationListener列表)

webEnvironment的过程

判断是否为web环境,主要是看有没有加载javax.servlet.Servletorg.springframework.web.context.ConfigurableWebApplicationContext相关的类
另外,在DemoApplication的main方法中手动设置webEnvironment也会直接生效,如下:

1
2
3
4
5
public static void main(String[] args) {
SpringApplication application = new SpringApplication(DemoApplication.class);
application.setWebEnvironment(false);
application.run(args);
}

设置初始化类

设置初始化类是通过SpringApplication中的方法getSpringFactoriesInstances(),参数为 ApplicationContextInitializer.class

1
2
3
4
5
6
7
8
9
10
11
private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type,
Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// Use names and ensure unique to protect against duplicates
Set<String> names = new LinkedHashSet<String>(
SpringFactoriesLoader.loadFactoryNames(type, classLoader));
List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}

SpringFactoriesLoader.loadFactoryNames方法,获取了spring-boot包下的META-INF/spring.factories中的ApplicationListeners,主要包括:

1
2
3
4
5
6
7
8
9
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

//TODO

run方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
analyzers = new FailureAnalyzers(context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
}

======== 未完待续 =========