网站首页 > 资源文章 正文
提问
- 服务启动的时候,SpringApplication内部做了什么?
- 创建上下文的时候是使用的哪一种ApplicationContext?
- Bean 是在哪个步骤定义的,BeanDefinition怎么排序的?
- Bean 是在哪个步骤创建和初始化的?
- 监听器有什么作用,都发布了哪些事件?
本篇文章主要介绍Bean是什么时候定义的,及Bean是如何创建的。
实例讲解
application.yml
client:
id: 1
server: localhost
MyClientAutoConfiguration
@Configuration
@EnableConfigurationProperties(ClientProperties.class)
@ConditionalOnProperty(prefix = "client", name = "enable", havingValue = "true")
public class MyClientAutoConfiguration {
?
private ClientProperties properties;
@Autowired
public MyClientAutoConfiguration(ClientProperties properties) {
this.properties = properties;
}
?
@Bean
public MyClient client1() {
return new MyClient(1);
}
?
?
@Configuration
@ConditionalOnProperty(name = "client.valid", havingValue = "true", matchIfMissing = true)
static class ClientConfiger {
?
@Bean
public MyClient client2() {
return new MyClient(2);
}
?
@Configuration
static class MyClientConfiger {
private final MyClient client;
?
@Autowired
public MyClientConfiger(MyClient client) {
this.client = client;
}
}
}
}
ConditionalBootstrap
@SpringBootApplication
public class ConditionalBootStrap {
?
public static void main(String[] args) {
SpringApplication.run(ConditionalBootStrap.class, args);
}
}
当服务启动的时候,大家可以先猜测一下,哪些Bean会被注册(registerBeanDefinition)?
结合源码讲解
让我们直接达到 SpringApplication.run 中的 refreshContext 方法,看看里面做了什么?
@Override
public void refresh() throws BeansException, IllegalStateException {
...
?
try {
...
?
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
?
...
?
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
?
}
从源码中我们看看这两个方法主要是完成什么工作的。
invokeBeanFactoryPostProcessors
Instantiate and invoke all registered BeanFactoryPostProcessor beans, respecting explicit order if given. Must be called before singleton instantiation
这个方法主要完成BeanDefinition的工作:
- 哪些Class 实现了 registerBeanDefinition?
- BeanDefinitions 的顺序是什么样的?
提示:到refreshContext这一步之前,有些内置的类和 primarySource(即 ConditionalBootStrap)已经放到了BeanDefinitionMap中
哪些Class 实现了 registerBeanDefinition?
- 首先会将之前的BeanDefinitionNames 循环,然后找出 @Configuration Class (isFullConfigurationClass || isLiteConfigurationClass)
- 解析每一个 @Configuration 类
通过ConfigurationClassParser#parse 解析上面的configCandidates:
- 首先判断这个类是否有效,是否应该忽略,这个地方有个很重要的提示,目前版本Spring/SpringBoot 都是通过 ConditionEvaluator#shouldSkip 来判断一个类是否应该忽略(Determine if an item should be skipped based on {@code @Conditional} annotations)
- 该类如果不应该忽略时,就会继续寻找该类是否有 @ComponentScan 注解(我们知道@SpringBootApplication 注解也包含 @Component注解)
- 找到 @ComponentScan 注解后,使用 ComponentScanAnnotationParser#parse 完成 basePackages 下所有类的扫描(如果@ComponentScan中没有加basePackages,默认会使用primarySource类所在包为basePackages)
- 获取basePackages下的所有类(getResources)
- 循环每个类,并且判断是否是有效的没有被排除(不在excludeFilter内)是@Component类,或者是派生类(@Configuration等)而且不应该被跳过,即 ConditionEvaluator#shouldSkip = false
- 将有效的类进行 registerBeanDefinition
到这一步时,可以猜一下上面实例中哪些类是有效的
这里你有没有疑问:
问:为什么MyClientAutoConfiguration类是无效的?
答:因为@ConditionalOnProperty(prefix = "client", name = "enable", havingValue = "true") 不满足条件,yml中没有 client.enable=true,所以 ConditionEvaluator#shouldSkip=true,就被跳过了。
问:为什么内部类 MyClientConfiger 和 ClientConfiger是有效的?
答:因为这两个类都是 @Configuration Class ,而且 ConditionEvaluator#shouldSkip=false ;@ConditionalOnProperty(matchIfMissing = true) 中 matchIfMissing=true的意思是如果没有找到匹配的也放行。
- 获取BeanMethod
所谓BeanMethod 就是标有@Bean的方法
上面实例中标有@Bean 的方法有:
MyClientAutoConfiguration#client1 方法 和 MyClientAutoConfiguration$ClientConfiger#client2 方法,但是因为MyClientAutoConfiguration类不符合条件,所以client1 bean method也是无效的,到这里获取到的 BeanMethod 只有client2
将此BeanMethod进行注册,即registry.registerBeanDefinition(beanName, beanDefToRegister)。
到目前为止在此过程中注册了3个BeanDefinition, 即MyClientConfiger 和 ClientConfiger ,及client2,那么MyClientConfiger 和 ClientConfiger的顺序是怎么控制的?
BeanDefinitions 的顺序是什么样的?
在上面步骤中我们说了,获取basePackages下的所有类(getResources),那么Class的顺序就是在getResources 过程中改变:
protected File[] listDirectory(File dir) {
File[] files = dir.listFiles();
if (files == null) {
if (logger.isInfoEnabled()) {
logger.info("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
}
return new File[0];
}
Arrays.sort(files, Comparator.comparing(File::getName));
return files;
}
从上面源码中我们可以看到,如果是文件,那么是根据文件名进行排序的,排序后变成
那么如果是Jar文件呢,大家可以看下源码,是根据JarFile解压之后的顺序。
finishBeanFactoryInitialization
此方法主要完成BeanDefinition -> Bean的过程。
这里或按照顺序依次遍历之前的 BeanDefinitions,然后进行 getBean -> createBean -> initialize ,但是有个地方需要注意就是当一个Bean创建/初始化过程中如果需要/依赖其他的Bean,且这个依赖的Bean 还没有创建的时候,则优先会创建这个Bean(doResolveDependency),比如:
@Autowired
public MyClientConfiger(MyClient client) {
this.client = client;
}
MyClientConfiger 在创建Bean初始化过程中,发现构造函数中需要依赖 MyClient 类型的Bean ,此时就会优先创建MyClient Bean ,即beanName=client2,如果没有找到这个BeanDefinition,则此时就会报错 throw new NoSuchBeanDefinitionException 。
好了,Bean定义和创建的过程已经讲完了,下面我抛一个问题给大家,假如我把 client.enable=true 配置加上会发生什么?
client:
id: 1
server: localhost
enable: true
猜你喜欢
- 2024-10-19 组件库Lerna Monorepo、Vite 和 Storybook
- 2024-10-19 STM32CubeMX教程1---安装与使用(stm32cubeide安装)
- 2024-10-19 玩转群晖NAS,影音篇:神级下载工具Transmission,及配置
- 2024-10-19 基于Sublime Text编辑器配置Python解释器
- 2024-10-19 搭建内网Linux CentOS yum源,摆脱依赖包困扰
- 2024-10-19 R语言实战—自学笔记—入门(r语言入门经典)
- 2024-10-19 如何使用逻辑回归从头开始创建分类器
- 2024-10-19 前端多包管理工具lerna使用详解(前端包管理器)
- 2024-10-19 如何在一个工程下管理多个npm包?多包管理工具lerna了解一下
- 2024-10-19 Python3基础之构建setup.py(python 构建)
你 发表评论:
欢迎- 最近发表
-
- YouTube应用下载全攻略:安卓、iOS及视频下载指南
- 谷歌浏览器Chrome 38.0.2125.101稳定版下载
- 谷歌浏览器(Chrome)官方网站下载地址
- 谷歌浏览器 Chrome v78.0.3904.108 正式版发布(附下载地址)
- 抛弃Windows吧!谷歌推免费Chrome系统,一个U盘就搞定
- 微软免费AR手游《我的世界Earth》上架:仅66MB
- 三星Note4升级安卓6.0.1出现怪异现象,求大神支招解决
- 红米k40手机4*1天气插件(红米k40pro天气设置到桌面)
- 一加11拆解:隐藏在强悍性能下的还有你不知道的细节
- 三星Galaxy Note 4/Edge 直升安卓5.0.1
- 标签列表
-
- 电脑显示器花屏 (79)
- 403 forbidden (65)
- linux怎么查看系统版本 (54)
- 补码运算 (63)
- 缓存服务器 (61)
- 定时重启 (59)
- plsql developer (73)
- 对话框打开时命令无法执行 (61)
- excel数据透视表 (72)
- oracle认证 (56)
- 网页不能复制 (84)
- photoshop外挂滤镜 (58)
- 网页无法复制粘贴 (55)
- vmware workstation 7 1 3 (78)
- jdk 64位下载 (65)
- phpstudy 2013 (66)
- 卡通形象生成 (55)
- psd模板免费下载 (67)
- shift (58)
- localhost打不开 (58)
- 检测代理服务器设置 (55)
- frequency (66)
- indesign教程 (55)
- 运行命令大全 (61)
- ping exe (64)
本文暂时没有评论,来添加一个吧(●'◡'●)