Spring 源码解析十二:SpringBoot 的加载机制

x33g5p2x  于2022-02-11 发布在 Spring  
字(14.2k)|赞(0)|评价(0)|浏览(496)

1. spring-boot 包含的模块

在解析 SpringBoot 的加载机制之前,先来看看官方 spring-boot 包含有哪些模块,各有什么用。

spring-boot 官方仓库

  • spring-boot:SpringBoot 的核心包,包括对 spring-framework 的封装与扩展、application.yaml 加载机制、注解启动机制等
  • spring-boot-autoconfigureapplication.yaml 的配置项定义
  • spring-boot-actuator:对 Spring 的健康检查、审计、统计和监控
  • spring-boot-actuator-autoconfigure:对 Spring 健康检查、审计、统计和监控的 application.yaml 配置项支持
  • spring-boot-cli:命令行执行支持
  • spring-boot-devtools:开发时监听 class 文件变动自动刷新应用程序
  • spring-boot-starters/*:SpringBoot starter,下面的项目全部是依赖定义,没有代码
  • spring-boot-tools/*:SpringBoot 配套工具

2. 应用入口

一个打包好的应用有如下的基本目录结构

  1. - /
  2. - BOOT-INF
  3. - classes # 存放应用的class文件与Resources资源文件
  4. - lib # 依赖的jar
  5. - META-INF
  6. - MANIFEST.MF # 应用的启动配置
  7. - org/springframework/boot/loader/ # spring-boot-loader 代码

我们一般可以使用 java -jar app.jar 来启动应用,MANIFEST.MF 就是来配置应用的启动

  1. # MANIFEST.MF
  2. Manifest-Version: 1.0
  3. Start-Class: com.example.demo.App
  4. Spring-Boot-Classes: BOOT-INF/classes/
  5. Spring-Boot-Lib: BOOT-INF/lib/
  6. Spring-Boot-Version: 2.1.4.RELEASE
  7. Main-Class: org.springframework.boot.loader.JarLauncher

对 Java 来说,这里面的配置,只有 Main-Class 是有效的,就是指定入口类

3. JarLauncher

JarLauncher
的主要功能是通过 jar 来启动应用

  1. public class JarLauncher extends ExecutableArchiveLauncher {
  2. // 入口启动方法
  3. public static void main(String[] args) throws Exception {
  4. new JarLauncher().launch(args);
  5. }
  6. }

现在来仔细分解这个类,先来看看继承关系

  1. - Launcher
  2. - ExecutableArchiveLauncher
  3. - JarLauncher

3.1. Launcher

Launcher
的主要功能是实现了基本的启动机制

  1. public abstract class Launcher {
  2. // 启动应用
  3. protected void launch(String[] args) throws Exception {
  4. // 获取类加载器
  5. ClassLoader classLoader = createClassLoader(getClassPathArchivesIterator());
  6. // 获取启动类
  7. String launchClass = getMainClass();
  8. // 启动
  9. launch(args, launchClass, classLoader);
  10. }
  11. // 启动
  12. protected void launch(String[] args, String launchClass, ClassLoader classLoader) throws Exception {
  13. Thread.currentThread().setContextClassLoader(classLoader);
  14. // 创建runner,run
  15. createMainMethodRunner(launchClass, args, classLoader).run();
  16. }
  17. // 创建runner
  18. protected MainMethodRunner createMainMethodRunner(String mainClass, String[] args, ClassLoader classLoader) {
  19. return new MainMethodRunner(mainClass, args);
  20. }
  21. // 获取启动类,由子类实现
  22. protected abstract String getMainClass() throws Exception;
  23. }

来看看 MainMethodRunner
是怎么运行的

  1. public class MainMethodRunner {
  2. private final String mainClassName;
  3. private final String[] args;
  4. public MainMethodRunner(String mainClass, String[] args) {
  5. this.mainClassName = mainClass;
  6. this.args = (args != null) ? args.clone() : null;
  7. }
  8. public void run() throws Exception {
  9. Class<?> mainClass = Class.forName(this.mainClassName, false, Thread.currentThread().getContextClassLoader());
  10. // 获取mainClass的main方法
  11. Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
  12. mainMethod.setAccessible(true);
  13. // 调用main方法
  14. mainMethod.invoke(null, new Object[] { this.args });
  15. }
  16. }

3.2. ExecutableArchiveLauncher

ExecutableArchiveLauncher
的主要功能是执行归档文件(jar, war)

  1. public abstract class ExecutableArchiveLauncher extends Launcher {
  2. // 启动类
  3. private static final String START_CLASS_ATTRIBUTE = "Start-Class";
  4. // 获取启动类
  5. @Override
  6. protected String getMainClass() throws Exception {
  7. // 获取归档文件(jar, war)中的`MANIFEST.MF`文件
  8. Manifest manifest = this.archive.getManifest();
  9. String mainClass = null;
  10. if (manifest != null) {
  11. // 获取Start-Class指定的类作为启动类
  12. mainClass = manifest.getMainAttributes().getValue(START_CLASS_ATTRIBUTE);
  13. }
  14. if (mainClass == null) {
  15. // 启动类不能为空
  16. throw new IllegalStateException("No 'Start-Class' manifest entry specified in " + this);
  17. }
  18. return mainClass;
  19. }
  20. }

3.3. JarLauncher

JarLauncher
的主要功能是执行 jar 文件,并指定 classpath

  1. public class JarLauncher extends ExecutableArchiveLauncher {
  2. // 是否可以加载这下面的文件
  3. static final EntryFilter NESTED_ARCHIVE_ENTRY_FILTER = (entry) -> {
  4. if (entry.isDirectory()) {
  5. return entry.getName().equals("BOOT-INF/classes/");
  6. }
  7. return entry.getName().startsWith("BOOT-INF/lib/");
  8. };
  9. // "BOOT-INF/classes/"与"BOOT-INF/lib/*.jar"当做classpath加载
  10. @Override
  11. protected boolean isNestedArchive(Archive.Entry entry) {
  12. return NESTED_ARCHIVE_ENTRY_FILTER.matches(entry);
  13. }
  14. }

4. 启动应用

我们来看一个最基本的启动

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. @SpringBootApplication
  4. public class App {
  5. public static void main(String[] args) {
  6. SpringApplication.run(App.class, args);
  7. }
  8. }

SpringApplication
run 方法由于启动一个 SpringBoot 应用

  1. public class SpringApplication {
  2. public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
  3. // 指定启动主源,运行参数
  4. return new SpringApplication(primarySources).run(args);
  5. }
  6. }

5. SpringApplication.run

  1. public class SpringApplication {
  2. public SpringApplication(Class<?>... primarySources) {
  3. this(null, primarySources);
  4. }
  5. public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
  6. // 资源加载器
  7. this.resourceLoader = resourceLoader;
  8. // 启动主源
  9. this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
  10. // 获取 spring.factories 文件中定义的,实现Bootstrapper接口的组件
  11. this.bootstrappers = new ArrayList<>(getSpringFactoriesInstances(Bootstrapper.class));
  12. // 获取 spring.factories 文件中定义的,实现ApplicationContextInitializer接口的组件
  13. setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
  14. // 获取 spring.factories 文件中定义的,实现ApplicationListener接口的组件
  15. setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
  16. // 获取入口类文件,如com.example.demo.App.class
  17. this.mainApplicationClass = deduceMainApplicationClass();
  18. }
  19. // 运行项目
  20. public ConfigurableApplicationContext run(String... args) {
  21. // 创建启动上下文
  22. DefaultBootstrapContext bootstrapContext = createBootstrapContext();
  23. // 应用上下文
  24. ConfigurableApplicationContext context = null;
  25. // ... 代码省略
  26. // 获取 spring.factories 文件中定义的,实现SpringApplicationRunListener接口的组件
  27. SpringApplicationRunListeners listeners = getRunListeners(args);
  28. // 启动监听器
  29. listeners.starting(bootstrapContext, this.mainApplicationClass);
  30. try {
  31. // 应用参数
  32. ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
  33. // 准备环境对象
  34. ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
  35. // 初始化spring.beaninfo.ignore配置
  36. configureIgnoreBeanInfo(environment);
  37. // 创建应用上下文,默认使用 AnnotationConfigServletWebServerApplicationContext
  38. context = createApplicationContext();
  39. // 准备应用上下文
  40. prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
  41. // 调用context的refresh方法,刷新上下文数据
  42. refreshContext(context);
  43. // ... 代码省略
  44. // 调用应用开始的监听函数
  45. listeners.started(context);
  46. // 运行应用
  47. callRunners(context, applicationArguments);
  48. }
  49. catch (Throwable ex) {
  50. // ... 代码省略
  51. }
  52. try {
  53. // 调用应用运行的监听函数
  54. listeners.running(context);
  55. }
  56. catch (Throwable ex) {
  57. // ... 代码省略
  58. }
  59. return context;
  60. }
  61. // 创建启动上下文
  62. private DefaultBootstrapContext createBootstrapContext() {
  63. // 默认使用DefaultBootstrapContext,调用bootstrappers中每个组件的intitialize方法初始化
  64. DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
  65. this.bootstrappers.forEach((initializer) -> initializer.intitialize(bootstrapContext));
  66. return bootstrapContext;
  67. }
  68. }
  1. public class SpringApplication {
  2. // 准备环境对象
  3. private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
  4. DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
  5. // 创建一个spring-web的标准环境
  6. ConfigurableEnvironment environment = getOrCreateEnvironment();
  7. // 配置环境
  8. configureEnvironment(environment, applicationArguments.getSourceArgs());
  9. // ... 代码省略
  10. // 配置 additionalProfiles
  11. configureAdditionalProfiles(environment);
  12. // 绑定环境与应用
  13. bindToSpringApplication(environment);
  14. // ... 代码省略
  15. return environment;
  16. }
  17. // 配置环境
  18. protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
  19. // 添加数据转换与格式化服务
  20. if (this.addConversionService) {
  21. ConversionService conversionService = ApplicationConversionService.getSharedInstance();
  22. environment.setConversionService((ConfigurableConversionService) conversionService);
  23. }
  24. // 配置命令行的属性来源 propertySources
  25. configurePropertySources(environment, args);
  26. }
  27. }
  1. public class SpringApplication {
  2. // 准备应用上下文
  3. private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,
  4. ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
  5. ApplicationArguments applicationArguments, Banner printedBanner) {
  6. // 设置环境
  7. context.setEnvironment(environment);
  8. // 添加bean名字生成器、资源加载器、数据转换服务
  9. postProcessApplicationContext(context);
  10. // 调用initializers中组件的initialize方法
  11. applyInitializers(context);
  12. // ... 代码省略
  13. // 获取bean工厂
  14. ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
  15. // 注册springApplicationArguments单例
  16. beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
  17. // ... 代码省略
  18. // 获取所有的源
  19. Set<Object> sources = getAllSources();
  20. // 加载这些源下的资源
  21. load(context, sources.toArray(new Object[0]));
  22. // ... 代码省略
  23. }
  24. // 加载这些源下的资源
  25. protected void load(ApplicationContext context, Object[] sources) {
  26. // 创建BeanDefinitionLoader
  27. BeanDefinitionLoader loader = createBeanDefinitionLoader(getBeanDefinitionRegistry(context), sources);
  28. // ... 代码省略
  29. // 加载资源
  30. loader.load();
  31. }
  32. }
  1. public class SpringApplication {
  2. // 运行应用
  3. private void callRunners(ApplicationContext context, ApplicationArguments args) {
  4. List<Object> runners = new ArrayList<>();
  5. // 应用运行器ApplicationRunner
  6. runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
  7. // 命令行运行器CommandLineRunner
  8. runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
  9. for (Object runner : new LinkedHashSet<>(runners)) {
  10. if (runner instanceof ApplicationRunner) {
  11. // 调用ApplicationRunner.run方法
  12. callRunner((ApplicationRunner) runner, args);
  13. }
  14. if (runner instanceof CommandLineRunner) {
  15. // 调用CommandLineRunner.run方法
  16. callRunner((CommandLineRunner) runner, args);
  17. }
  18. }
  19. }
  20. }

这一小节中,有几点需要接下来慢慢解析

  • AnnotationConfigServletWebServerApplicationContext 如何管理应用与数据
  • BeanDefinitionLoader.load 如何加载资源

6. AnnotationConfigServletWebServerApplicationContext

AnnotationConfigServletWebServerApplicationContext
是 SpringBoot 默认使用应用上下文

先看看继承关系

  1. - GenericWebApplicationContext
  2. - ServletWebServerApplicationContext
  3. - AnnotationConfigServletWebServerApplicationContext

GenericWebApplicationContext
在 Spring 源码解析二:上下文组件(WebApplicationContext) 中已经讲过了,先来看看 ServletWebServerApplicationContext

6.1. ServletWebServerApplicationContext

ServletWebServerApplicationContext
的主要功能是可以启动自身为一个应用服务

  1. public class ServletWebServerApplicationContext extends GenericWebApplicationContext
  2. implements ConfigurableWebServerApplicationContext {
  3. // 刷新应用
  4. @Override
  5. protected void onRefresh() {
  6. super.onRefresh();
  7. // 刷新的时候,重新创建一个应用服务
  8. createWebServer();
  9. }
  10. // 创建一个应用服务
  11. private void createWebServer() {
  12. WebServer webServer = this.webServer;
  13. ServletContext servletContext = getServletContext();
  14. // 应用服务与上下文都为空
  15. if (webServer == null && servletContext == null) {
  16. // 获取 ServletWebServerFactory 的bean,内置实现有tomcat、jetty、undertow、netty
  17. ServletWebServerFactory factory = getWebServerFactory();
  18. // 当应用服务初始化完成后,调用自身的selfInitialize
  19. this.webServer = factory.getWebServer(getSelfInitializer());
  20. // ... 代码省略
  21. }
  22. // 如果有上下文对象,调用自身的selfInitialize来初始化
  23. else if (servletContext != null) {
  24. try {
  25. getSelfInitializer().onStartup(servletContext);
  26. }
  27. catch (ServletException ex) {
  28. // ... 代码省略
  29. }
  30. }
  31. // ... 代码省略
  32. }
  33. }
  1. public class ServletWebServerApplicationContext extends GenericWebApplicationContext
  2. implements ConfigurableWebServerApplicationContext {
  3. // 初始化应用
  4. private void selfInitialize(ServletContext servletContext) throws ServletException {
  5. // ... 代码省略
  6. // 获取所有ServletContextInitializer的bean,调用这些组件的onStartup方法
  7. for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
  8. beans.onStartup(servletContext);
  9. }
  10. }
  11. }

6.2. AnnotationConfigServletWebServerApplicationContext

AnnotationConfigServletWebServerApplicationContext
的主要功能是自动扫描注解定义的 bean

  1. public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext
  2. implements AnnotationConfigRegistry {
  3. // 注解bean定义读取器
  4. private final AnnotatedBeanDefinitionReader reader;
  5. // 类路径bean定义扫描器
  6. private final ClassPathBeanDefinitionScanner scanner;
  7. // 已注册的注解类组件
  8. private final Set<Class<?>> annotatedClasses = new LinkedHashSet<>();
  9. // 待扫描的包
  10. private String[] basePackages;
  11. public AnnotationConfigServletWebServerApplicationContext() {
  12. // 初始化读取器与扫描器
  13. this.reader = new AnnotatedBeanDefinitionReader(this);
  14. this.scanner = new ClassPathBeanDefinitionScanner(this);
  15. }
  16. public AnnotationConfigServletWebServerApplicationContext(DefaultListableBeanFactory beanFactory) {
  17. super(beanFactory);
  18. // 初始化读取器与扫描器
  19. this.reader = new AnnotatedBeanDefinitionReader(this);
  20. this.scanner = new ClassPathBeanDefinitionScanner(this);
  21. }
  22. public AnnotationConfigServletWebServerApplicationContext(Class<?>... annotatedClasses) {
  23. this();
  24. // 注册bean,并刷新应用上下文数据
  25. register(annotatedClasses);
  26. refresh();
  27. }
  28. public AnnotationConfigServletWebServerApplicationContext(String... basePackages) {
  29. this();
  30. // 扫描包,并刷新应用上下文数据
  31. scan(basePackages);
  32. refresh();
  33. }
  34. }
  1. public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext
  2. implements AnnotationConfigRegistry {
  3. // 注册bean
  4. @Override
  5. public final void register(Class<?>... annotatedClasses) {
  6. this.annotatedClasses.addAll(Arrays.asList(annotatedClasses));
  7. }
  8. // 扫描包
  9. @Override
  10. public final void scan(String... basePackages) {
  11. this.basePackages = basePackages;
  12. }
  13. // 应用上下文刷新完之后,自动加载bean与扫描包
  14. @Override
  15. protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  16. super.postProcessBeanFactory(beanFactory);
  17. if (this.basePackages != null && this.basePackages.length > 0) {
  18. this.scanner.scan(this.basePackages);
  19. }
  20. if (!this.annotatedClasses.isEmpty()) {
  21. this.reader.register(ClassUtils.toClassArray(this.annotatedClasses));
  22. }
  23. }
  24. }

7. BeanDefinitionLoader.load

在上面的代码中,资源加载是类 BeanDefinitionLoader
完成的

  1. class BeanDefinitionLoader {
  2. // 加载资源
  3. void load() {
  4. // 遍历加载
  5. for (Object source : this.sources) {
  6. load(source);
  7. }
  8. }
  9. // 加载单个资源
  10. private void load(Object source) {
  11. // 加载类下的资源
  12. if (source instanceof Class<?>) {
  13. loadClass((Class<?>) source);
  14. return;
  15. }
  16. // 加载Resource的资源
  17. if (source instanceof Resource) {
  18. loadResource((Resource) source);
  19. return;
  20. }
  21. // 加载包下的资源
  22. if (source instanceof Package) {
  23. loadPackage((Package) source);
  24. return;
  25. }
  26. // 加载字符代表的资源
  27. if (source instanceof CharSequence) {
  28. loadCharSequence((CharSequence) source);
  29. return;
  30. }
  31. // 其他不能加载
  32. throw new IllegalArgumentException("Invalid source type " + source.getClass());
  33. }
  34. }
  1. class BeanDefinitionLoader {
  2. // 加载类下的资源
  3. private void loadClass(Class<?> source) {
  4. // 加载类下的注解
  5. this.annotatedReader.register(source);
  6. }
  7. // 加载Resource的资源
  8. private void loadResource(Resource source) {
  9. // 加载Resource的xml
  10. this.xmlReader.loadBeanDefinitions(source);
  11. }
  12. // 加载包下的资源
  13. private void loadPackage(Package source) {
  14. // 扫描包下的注解
  15. this.scanner.scan(source.getName());
  16. }
  17. // 加载字符代表的资源
  18. private void loadCharSequence(CharSequence source) {
  19. // 替换占位符${}
  20. String resolvedSource = this.scanner.getEnvironment().resolvePlaceholders(source.toString());
  21. // 当做类加载,如果成功,返回
  22. try {
  23. load(ClassUtils.forName(resolvedSource, null));
  24. return;
  25. }
  26. catch (IllegalArgumentException | ClassNotFoundException ex) {}
  27. // 当做Resource加载,如果成功,返回
  28. if (loadAsResources(resolvedSource)) {
  29. return;
  30. }
  31. // 当做包加载,如果成功,返回
  32. Package packageResource = findPackage(resolvedSource);
  33. if (packageResource != null) {
  34. load(packageResource);
  35. return;
  36. }
  37. // 其他不能加载
  38. throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'");
  39. }
  40. }

后续

更多博客,查看 https://github.com/senntyou/blogs

作者:深予之 (@senntyou)

版权声明:自由转载-非商用-非衍生-保持署名(创意共享 3.0 许可证

相关文章