在Spring Boot框架中,最先接触的应该就是SpringApplication这个类。所以源码分析也先从这个类开始。
SpringApplication用来从Java的main方法中启动Spring Boot应用。他的行为包括:
- 创建适当的应用上下文实例
- 对应CommandLinePropertySource和Spring的参数
- 初始化应用上下文,并且加载单例Bean
- 触发CommandLineRunner
其中,我们先看初始化方法。SpringApplication一共包含两个构造方法,两个构造方法均会调用initialize函数。
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();
}
这个方法中,首先deduceWebEnvironment检验网络环境。具体方法是检查默认类加载器是否加载过Servlet和ConfigurableWebApplicationContext这两个类。如果加载过,那么即为Web应用。
第二步,getSpringFactoriesInstances方法接受ApplicationContextInitializer作为参数。然后一直调用到getSpringFactoriesInstances方法。
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;
}
在getSpringFactoriesInstances方法内部,首先会调用getContextClassLoader。这里对应的类加载器为加载器AppClassLoader,负责加载java.class.path下的文件,其父加载器为ext类加载器,关系是:null(BootstrapClassLoader非java实现所以为空)->ext class loader->app class loader。至于为什使用ContextClassLoader,Spring boot中提到:
Launched applications should use Thread.getContextClassLoader() when loading classes(most libraries and frameworks will do this by default). Trying to load nested jarclasses viaClassLoader.getSystemClassLoader() will fail.
获得classloader的方法包括:
ClassLoader.getSystemClassLoader()
Thread.getContextClassLoader()
Test.class.getClassLoader()
至于使用getContextClassLoader的具体的原因,下文提到,systemClassLoader仅仅适用于简单的程序。web应用下一定会出错:
http://www.javaworld.com/article/2077344/core-java/find-a-way-out-of-the-classloader-maze.html。
要加载的类是ApplicationContextInitializer.class, 在springframework.context包下,这一类nested jar中的类,无法用systemClassLoader。因为systemClassLoader仅仅加载CLASSPATH路径下的类。无法加载nested jar中的类。如上文所写,获取classloader的一般方法为class.getClassLoader和getContextClassLoader。
之后使用ContextClassLoader加载spring.factories并找到配置文件路径,并从中找到ApplicationContextInitializer路径然后加载。之后用相同的方法加载ApplicationListen

本文主要分析了Spring Boot中SpringApplication的源码,包括它的构造方法、初始化过程、ApplicationContextInitializer的加载、ApplicationListener的处理,以及refresh方法在启动过程中的作用。通过探讨getClassLoader的选择、加载spring.factories文件和配置应用上下文,揭示了Spring Boot启动的核心步骤。
最低0.47元/天 解锁文章
1195

被折叠的 条评论
为什么被折叠?



