springboot启动流程 (3) 自动装配

news2024/9/29 23:39:04

在SpringBoot中,EnableAutoConfiguration注解用于开启自动装配功能。

本文将详细分析该注解的工作流程。

EnableAutoConfiguration注解

启用SpringBoot自动装配功能,尝试猜测和配置可能需要的组件Bean。

自动装配类通常是根据类路径和定义的Bean来应用的。例如,如果类路径上有tomcat-embedded.jar,那么可能需要一个TomcatServletWebServerFactory(除非已经定义了自己的Servlet WebServerFactory Bean)。

自动装配试图尽可能地智能化,并将随着开发者定义自己的配置而取消自动装配相冲突的配置。开发者可以使用exclude()排除不想使用的配置,也可以通过spring.autoconfig.exclude属性排除这些配置。自动装配总是在用户定义的Bean注册之后应用。

用@EnableAutoConfiguration注解标注的类所在包具有特定的意义,通常用作默认扫描的包。通常建议将@EnableAutoConfiguration(如果没有使用@SpringBootApplication注解)放在根包中,以便可以搜索所有子包和类。

自动装配类是普通的Spring @Configuration类,使用SpringFactoriesLoader机制定位。通常使用@Conditional方式装配,最常用的是@ConditionalOnClass和@ConditionalOnMissingBean注解。

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

	/**
	 * Exclude specific auto-configuration classes such that they will never be applied.
	 */
	Class<?>[] exclude() default {};

	/**
	 * Exclude specific auto-configuration class names such that they will never be
	 * applied.
	 * 当类路径下没有指定的类时,可以使用这个属性指定排除的类
	 */
	String[] excludeName() default {};
}

该注解Import了AutoConfigurationImportSelector类,AutoConfigurationImportSelector类实现了DeferredImportSelector接口。

Import注解和DeferredImportSelector接口在之前的"Spring @Import注解源码分析"中详细分析过,此处在介绍它们,只分析AutoConfigurationImportSelector的工作流程。

AutoConfigurationImportSelector类

DeferredImportSelector接口

A variation of ImportSelector that runs after all @Configuration beans have been processed. This type of selector can be particularly useful when the selected imports are @Conditional.

Implementations can also extend the org.springframework.core.Ordered interface or use the org.springframework.core.annotation.Order annotation to indicate a precedence against other DeferredImportSelectors.

Implementations may also provide an import group which can provide additional sorting and filtering logic across different selectors.

AutoConfigurationGroup类

AutoConfigurationImportSelector的getImportGroup方法返回了AutoConfigurationGroup类。

private static class AutoConfigurationGroup implements 
		DeferredImportSelector.Group, BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware {

	private final Map<String, AnnotationMetadata> entries = new LinkedHashMap<>();

	private final List<AutoConfigurationEntry> autoConfigurationEntries = new ArrayList<>();

	// ... 略

	@Override
	public void process(
			AnnotationMetadata annotationMetadata,
			DeferredImportSelector deferredImportSelector) {

		// AutoConfigurationEntry类使用List保存Configuration类
		AutoConfigurationEntry autoConfigurationEntry =
            ((AutoConfigurationImportSelector) deferredImportSelector)
				.getAutoConfigurationEntry(annotationMetadata);

		this.autoConfigurationEntries.add(autoConfigurationEntry);
		for (String importClassName : autoConfigurationEntry.getConfigurations()) {
			this.entries.putIfAbsent(importClassName, annotationMetadata);
		}
	}

	@Override
	public Iterable<Entry> selectImports() {
		// 查找排除的配置类
		Set<String> allExclusions = this.autoConfigurationEntries.stream()
				.map(AutoConfigurationEntry::getExclusions)
				.flatMap(Collection::stream)
				.collect(Collectors.toSet());
		// 所有配置类
		Set<String> processedConfigurations = this.autoConfigurationEntries.stream()
				.map(AutoConfigurationEntry::getConfigurations)
				.flatMap(Collection::stream)
				.collect(Collectors.toCollection(LinkedHashSet::new));
		// 将排除的配置类移除掉
		processedConfigurations.removeAll(allExclusions);

		// 排序
		return sortAutoConfigurations(processedConfigurations, getAutoConfigurationMetadata()).stream()
				.map((importClassName) -> new Entry(this.entries.get(importClassName), importClassName))
				.collect(Collectors.toList());
	}

	// ... 略
}

从上面的代码可以看出,查找自动装配类的逻辑在getAutoConfigurationEntry方法中。

getAutoConfigurationEntry方法

从META-INF/spring.factories文件解析EnableAutoConfiguration配置。

META-INF/spring.factories文件示例:

在这里插入图片描述

protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
	AnnotationAttributes attributes = getAttributes(annotationMetadata);
	// 查找自动装配类
	List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
	// 以下几行为查找排除类、过滤等操作
	configurations = removeDuplicates(configurations);
	Set<String> exclusions = getExclusions(annotationMetadata, attributes);
	checkExcludedClasses(configurations, exclusions);
	configurations.removeAll(exclusions);
	// 这里的Filter是从META-INF/spring.factories文件解析出来的
	configurations = getConfigurationClassFilter().filter(configurations);
	// 触发事件
	fireAutoConfigurationImportEvents(configurations, exclusions);
	return new AutoConfigurationEntry(configurations, exclusions);
}

protected List<String> getCandidateConfigurations(
		AnnotationMetadata metadata, AnnotationAttributes attributes) {
	// 从META-INF/spring.factories文件查找EnableAutoConfiguration配置
	List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
						getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
	return configurations;
}

SpringFactoriesLoader类loadFactoryNames方法

Load the fully qualified class names of factory implementations of the given type from “META-INF/spring.factories”, using the given class loader.

public static List<String> loadFactoryNames(Class<?> factoryType, ClassLoader classLoader) {
	String factoryTypeName = factoryType.getName();
	return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}

private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
	MultiValueMap<String, String> result = cache.get(classLoader);
	if (result != null) {
		return result;
	}

	try {
		// FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"
		// 从类路径下查找META-INF/spring.factories文件
		Enumeration<URL> urls = (classLoader != null ?
				classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
				ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
		result = new LinkedMultiValueMap<>();
		while (urls.hasMoreElements()) {
			URL url = urls.nextElement();
			UrlResource resource = new UrlResource(url);
			// 获取properties配置
			Properties properties = PropertiesLoaderUtils.loadProperties(resource);
			for (Map.Entry<?, ?> entry : properties.entrySet()) {
				String factoryTypeName = ((String) entry.getKey()).trim();
				for (String factoryImplementationName :
                     StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
					result.add(factoryTypeName, factoryImplementationName.trim());
				}
			}
		}
		// 把配置添加缓存
		cache.put(classLoader, result);
		return result;
	} catch (IOException ex) {
		throw new IllegalArgumentException("Unable to load factories from location [" +
				FACTORIES_RESOURCE_LOCATION + "]", ex);
	}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/670678.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

005 Settings可以直接通过AndroidStudio安装并调试(二)——Settings 打release包遇到的问题

一.背景 Settings迁移到AndroidStudio中直接打release包是有各种问题的,打不出来包,这里我们详细来描述下Settings打包出现的问题及解决方案。 二.Type com.android.settingslib.widget.BuildConfig is defined multiple times 首先遇到的拦路虎,也是最繁琐的包名冲突,之…

为什么配电室总出故障?这一点你做对了吗

配电室是供电系统中非常关键的组成部分&#xff0c;负责对电能进行分配和控制。然而&#xff0c;传统的配电室监控方式存在一些局限性&#xff0c;如人工巡检的局限性、监测数据获取困难、安全隐患无法及时发现等。 因此&#xff0c;为了提高配电室的管理水平、确保供电系统的安…

剑指offer03.数组中重复的数字

看到这道题的第一眼想到的是先给它排序&#xff0c;然后双指针从左往右遍历&#xff0c;写了一个冒泡排序&#xff0c;但是我想到了应该会超时&#xff0c;因为冒泡时间复杂度是n的平方&#xff0c;输入大小时10000&#xff0c;肯定会超时&#xff0c;然后右又看了一下题目看到…

SpringCloud Alibaba入门之用户子模块开发

在上一章的基础上进行子模块的开发SpringCloud Alibaba入门之创建多模块工程_qinxun2008081的博客-CSDN博客 一、引入SpringBoot 我们在父项目统一管理引入的jar包的版本。我们采用父项目中以depencyMangement方式引入spring-boot&#xff0c;子项目依赖parent父配置即可。 &…

YGG 公会发展计划(GAP)第三季总结

2023年5月6日&#xff0c;Yield Guild Games&#xff08;YGG&#xff09;结束了其代币分配系统——公会发展计划&#xff08;GAP&#xff09;的第三季&#xff0c;向社区成员提供了更多奖励&#xff0c;以表彰他们对公会和参与游戏的宝贵贡献。 第三季对 GAP 进行了一些升级&am…

uniapp和springboot微信小程序开发实战:前端架构之微信小程序开发表单提交功能

文章目录 前言前端代码后端代码controller层service层总结前言 基本上很多项目都有类似于意见反馈、留言等形式的表单提交功能,今天给大家介绍的是使用uniapp和vue组件实现的表单提交功能。其效果如下: 前端代码 <template><view class="body"><…

Jmeter接口测试-MD5加密-请求验签

目录 前言&#xff1a; 第一部分&#xff1a;先准备好Jmeter 第二部分&#xff1a;编写MD5加密-请求验签的脚本 第三部分&#xff1a;执行脚本 前言&#xff1a; JMeter是一款常用的接口测试工具&#xff0c;对于需要进行加密验证的接口&#xff0c;我们可以使用MD5加密算…

HOOPS Exchange SDK 2023 Crack

领先的 CAD 导入和导出库 使用 HOOPS Exchange SDK 将 30 多种 CAD 文件格式导入您的应用程序以进行 CAD 数据转换&#xff0c;通过单个 API 对 2D 和 3D CAD 文件格式&#xff08;包括 CATIA、SOLIDWORKS、 Inventor、Revit™™、Creo、NX™、Solid Edge 等&#xff09;提供快…

Nvidia官方解码性能

NVIDIA VIDEO CODEC SDK | NVIDIA Developer 1080P解码性能&#xff1a; 720P解码性能&#xff1a; 详细的参见官方的链接地址&#xff0c;对于GPU的解码fps能力&#xff0c;可以作为评估参照&#xff01;

【服务器远程工具】一款好用的xshell

这里写目录标题 背景Tabby简介安装使用SSHSFTPPowerShellGit 设置外观颜色快捷键窗口 插件支持总结 背景 作为一名后端开发&#xff0c;我们经常需要和Linux系统打交道&#xff0c;免不了要使用Xshell这类终端工具来进行远程管理。今天给大家推荐一款更炫酷的终端工具Tabby&…

C++核心编程——详解函数模板

纵有疾风起&#xff0c;人生不言弃。本文篇幅较长&#xff0c;如有错误请不吝赐教&#xff0c;感谢支持。 &#x1f4ac;文章目录 一.模板的基础知识①为什么有模板&#xff1f;②初识模板 二.函数模板①函数模板的定义②函数模板的使用③函数模板实例化1️⃣隐式实例化2️⃣显…

QAC用户使用手册

文章目录 1 QAC介绍1.1 QAC简介1.2 QAC dashboard简介 2 QAC使用&#xff08;基本操作&#xff09;2.1 创建QAC工程2.2 创建QAC工程2.3 添加代码到QAC工程2.4 添加代码到QAC工程2.5 上传分析报告及结果 1 QAC介绍 1.1 QAC简介 Helix QAC是Perforce公司(原PRQA公司)产品,主要用…

「Java核心技术大会 2023」——小解送书第三期

目录 共同深入探讨 Java 生态&#xff01;直播预约&#xff1a;视频号“IT阅读排行榜” 抽奖 大会简介 人工智能在22年、23年的再次爆发让Python成为编程语言里最大的赢家&#xff1b;云原生的持续普及令Go、Rust等新生的语言有了进一步叫板传统技术体系的资本与底气。我们必…

Android studio项目编译进安卓源码中

最近要做一个Android 8.1 的launcher &#xff0c;在Androidstudio上开发好基本功能后&#xff0c;移到Android源码中编译 1.在源码中创建代码目录 我开发基于展讯9820e平台&#xff0c;在如下目录创建好对应名字的文件夹 \vendor\sprd\platform\packages\apps\xxxLauncher创…

回收站清空了怎么恢复?3个妙招恢复数据

回收站被人为清空&#xff0c;被放入回收站的文件因时间过久而被电脑自动删除时&#xff0c;回收站里的数据清空了还能找到吗&#xff1f;是可以的这3个小妙招可以帮你还原回收站的数据&#xff01; 妙招一&#xff1a;借助注册表还原回收站清空的数据 可以尝试借助注册表还原…

Bootstrap 环境安装

文章目录 Bootstrap 环境安装下载 Bootstrap 文件结构预编译的 BootstrapBootstrap 源代码 HTML 模板实例Bootstrap CDN 推荐 Bootstrap 环境安装 Bootstrap 安装是非常容易的。本章将讲解如何下载并安装 Bootstrap&#xff0c;讨论 Bootstrap 文件结构&#xff0c;并通过一个实…

常见的Jmeter参数化方式总结

目录 前言&#xff1a; 参数化概念 参数化方式 二、用户变量 三、CSV数据文件 四、函数助手 前言&#xff1a; 在进行接口性能测试时&#xff0c;我们通常需要针对不同的场景进行参数化操作。JMeter是一款强大的性能测试工具&#xff0c;它提供了多种参数化方式&#xff0c;方便…

Idea在JavaSE项目中配置JavaEE

新建模块&#xff08;File --> new --> Module...&#xff09;javase项目 选择了这个webapp的支持之后&#xff0c;IDEA会自动给你生成一个符合Servlet规范的webpp目录结构。 如果说我们现在需要使用servlet的和JSP 那么需要servlet和JSP的jar包 也可以选择添加库,但是…

qt udp通信

udp不分客户端和服务器&#xff0c;只需要使用一个类 QUdpSocket 这里写目录标题 界面设计qudpsocketthis按钮 打开按钮 发送 关闭 界面设计 接收框设置为 只读 为ui界面各个模块改名字 本低端口和目标ip框对齐&#xff0c;可以对目标ip 宽度设置 为一样 水平策略 qudpsocke…

OpenHarmony端云一体化应用开发快速入门练习(下)登出销户等

一、登出 前提条件&#xff1a;需要在AGC控制台开通认证服务。需要先在您的应用中集成认证服务SDK。 开发步骤 当用户不再使用应用&#xff0c;或者需要使用其他帐号登录时&#xff0c;需要调用AGConnectAuth.signOut登出当前用户。用户一旦被登出&#xff0c;端侧的用户信息和…