当前位置: 首页 > news >正文

ApplicationContext在Spring Boot中是如何创建的?

一、ApplicationContext在Spring Boot中是如何创建的?

在这里插入图片描述

1. SpringApplication

ApplicationContextFactory有三个实现类,分别是AnnotationConfigReactiveWebServerApplicationContext.FactoryAnnotationConfigServletWebServerApplicationContext.Factory、DefaultApplicationContextFactory。

public ConfigurableApplicationContext run(String... args) {...// 创建ApplicationContextcontext = createApplicationContext();...
}// 调用DefaultApplicationContextFactory的create
protected ConfigurableApplicationContext createApplicationContext() {return this.applicationContextFactory.create(this.webApplicationType);
}

2. DefaultApplicationContextFactory

下面有一点代码SpringFactoriesLoader.loadFactories(ApplicationContextFactory.class, getClass().getClassLoader()),是从org.springframework.bootMETA-INF/spring.factories寻找ApplicationContextFactory的实现类,也就是AnnotationConfigReactiveWebServerApplicationContext.Factory和AnnotationConfigServletWebServerApplicationContext.Factory。

@Override
public ConfigurableApplicationContext create(WebApplicationType webApplicationType) {try {return getFromSpringFactories(webApplicationType, ApplicationContextFactory::create,AnnotationConfigApplicationContext::new);}catch (Exception ex) {throw new IllegalStateException("Unable create a default ApplicationContext instance, "+ "you may need a custom ApplicationContextFactory", ex);}
}private <T> T getFromSpringFactories(WebApplicationType webApplicationType,BiFunction<ApplicationContextFactory, WebApplicationType, T> action, Supplier<T> defaultResult) {for (ApplicationContextFactory candidate : SpringFactoriesLoader.loadFactories(ApplicationContextFactory.class,getClass().getClassLoader())) {// 判断应用属于三种NONE、SERVLET、REACTIVE类型中的哪种类型,实例化对应类型的ApplicationContenxt。T result = action.apply(candidate, webApplicationType);if (result != null) {return result;}}// 创建AnnotationConfigApplicationContextreturn (defaultResult != null) ? defaultResult.get() : null;
}

二、AnnotationConfigServletWebServerApplicationContext构造函数做了什么事情?

public AnnotationConfigServletWebServerApplicationContext() {this.reader = new AnnotatedBeanDefinitionReader(this);this.scanner = new ClassPathBeanDefinitionScanner(this);
}

1. AnnotatedBeanDefinitionReader

public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {Assert.notNull(registry, "BeanDefinitionRegistry must not be null");Assert.notNull(environment, "Environment must not be null");this.registry = registry;this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}

AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);注册了以下6个bean。

  • org.springframework.context.annotation.internalConfigurationAnnotationProcessor
  • org.springframework.context.annotation.internalAutowiredAnnotationProcessor
  • org.springframework.context.annotation.internalCommonAnnotationProcessor
  • org.springframework.context.annotation.internalPersistenceAnnotationProcessor
  • org.springframework.context.event.internalEventListenerProcessor
  • org.springframework.context.event.internalEventListenerFactory

2. ClassPathBeanDefinitionScanner

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,Environment environment, @Nullable ResourceLoader resourceLoader) {Assert.notNull(registry, "BeanDefinitionRegistry must not be null");this.registry = registry;if (useDefaultFilters) {registerDefaultFilters();}setEnvironment(environment);setResourceLoader(resourceLoader);
}

registerDefaultFilters 注册了以下的过滤器

protected void registerDefaultFilters() {this.includeFilters.add(new AnnotationTypeFilter(Component.class));// 这个ClassLoader是干什么的???ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();try {this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");}catch (ClassNotFoundException ex) {// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.}try {this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");}catch (ClassNotFoundException ex) {// JSR-330 API not available - simply skip.}
}
  • Component.class
  • javax.annotation.ManagedBean
  • javax.inject.Named
http://www.lryc.cn/news/116815.html

相关文章:

  • 后端开发7.轮播图模块【mongdb开发】
  • Linux常用命令(一):创建文件目录
  • 如何创建一个Vue组件?如何在父组件和子组件之间传递数据?如何在子组件中向父组件发送消息?
  • 设计模式之适配器模式
  • 让ChatGPT介绍一下ChatGPT(ChatGPT的自我介绍)
  • CentOS 7 构建 LVS-DR 群集
  • MySQL8.0.33二进制包安装与部署
  • RocketMQ发送消息失败:error CODE: 14 DESC: service not available now, maybe disk full
  • 1.Fay-UE5数字人工程导入(UE数字人系统教程)
  • Linux 终端操作命令(2)内部命令分类
  • 【数据结构与算法】十大经典排序算法-插入排序
  • 如何使用PHP Smarty进行条件判断和循环?
  • 使用svg生成图像
  • DNS、ARP
  • uniapp 微信小程序 echarts地图 点击显示类目
  • 速刷算法#Day-02
  • Java怎么手动将对象注入到springboot
  • twisted 18.7.0 requires PyHamcrest>=1.9.0 解决方案
  • 电脑关机程序
  • 构建之法 - 软工教学:每天都向前推进一点点
  • 基于Qlearning强化学习的路径规划算法matlab仿真
  • ASL国产CS5213 转VGA信号输出音频 替代AG6200安格芯片 HDMI to VGA(带音频)方案设计原理图
  • springboot启动忽略某些类
  • HCIA VLAN配置
  • 微信小程序--原生
  • Django快速上手
  • Android, 笔记+课表的app实现
  • Openlayers实战:多数据分散聚合
  • 9、Kubernetes核心技术 - Volume
  • HTML <small> 标签