迈向智能化:揭秘SpringBoot 3.x中SpringApplication.run和自动配置,实现方式和2.x有些不一样

SpringApplication.run

在这里插入图片描述

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		Web 应用程序类型 一般都是   SERVLET
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
		this.bootstrapRegistryInitializers = new ArrayList<>(
				getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
		设置初始化:ApplicationContextInitializer
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		设置 监听器 ApplicationListener
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		递归找到主函数main方法
		this.mainApplicationClass = deduceMainApplicationClass();
	}

在这里插入图片描述

自动配置原理

一个注解实现自动配置 @SpringBootApplication–>@EnableAutoConfiguration
在这里插入图片描述
在这里插入图片描述

点进去

在这里插入图片描述

介绍一个注解:@Import

简单来讲就是将某个或者某组类导入进来

AutoConfigurationImportSelector.class 这个类干了大事情

	protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return EMPTY_ENTRY;
		}
		获取属性
		AnnotationAttributes attributes = getAttributes(annotationMetadata);
		获取org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件里面的数据,共152条”数据“     重要
		List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
		去重
		configurations = removeDuplicates(configurations);
		获取排除项
		Set<String> exclusions = getExclusions(annotationMetadata, attributes);
		去重排除项
		checkExcludedClasses(configurations, exclusions);
		删除排除项
		configurations.removeAll(exclusions);
		重要 过滤当前工程内部需要配置Configuration类,如何判断?实现细节加以说明?
		configurations = getConfigurationClassFilter().filter(configurations);
		自动导入
		fireAutoConfigurationImportEvents(configurations, exclusions);
		return new AutoConfigurationEntry(configurations, exclusions);
	}

org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件里面的数据 与2.x路径和实现方式都不一样

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

过滤当前工程内部需要配置Configuration类,如何判断?实现细节加以说明?

如何判断?

在这里插入图片描述
----在这里插入图片描述

SpringFactoriesLoader.loadFactories(AutoConfigurationImportFilter.class, this.beanClassLoader) 什么意思

在这里插入图片描述

去这个文件找配置类

OnBeanCondition,\
OnClassCondition,\
OnWebApplicationCondition
简单介绍一下这两个OnClassCondition

它的作用是当类路径中存在指定的类时,才会进行条件匹配,从而决定是否启用某个配置。

##过滤代码
在这里插入图片描述
说明 :简单来讲就是:通过OnXXXCondition具体的实现类的配置文件

代码细节

在这里插入图片描述

对比:通过OnXXXCondition具体的实现类的配置文件

这个类不会被自动配置

在这里插入图片描述

WebMvcAutoConfiguration 这个类就会被自动配置

在这里插入图片描述

读后感:一篇手记、以后方便快速查找、或许可以帮助到其他人。写的比较粗糙 大概意思明白就行

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveCode.

永远年轻,永远热泪盈眶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值