spring boot AutoConfiguration.replacements 文件的作用

spring boot AutoConfiguration.replacements 文件的作用

自动配置类可能会在 @AutoConfigureBefore@AutoConfigureAfter 排序注解中被引用,也可能在 @SpringBootApplication(exclude =, excludeName = )@EnableAutoConfiguration(exclude = , excludeName = )spring.autoconfigure.exclude 属性值中被引用用于排除某些自动配置。

如果我们想要重命名自动配置类,或者将其移动到其他的包中,就会导致自动配置排序和排除失效。spring boot 提供了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.replacements 文件用于解决这个问题,在文件中写入旧类和新类的映射关系之后,spring boot 会先将注解和属性中的旧类名替换为新类名,再处理自动配置类的排序、排除。

例如:

旧类=新类

properties 复制代码
com.mycorp.libx.autoconfigure.LibXAutoConfiguration=com.mycorp.libx.autoconfigure.core.LibXAutoConfiguration

文件的读取由 AutoConfigurationReplacementsload 方法实现:

java 复制代码
package org.springframework.boot.autoconfigure;

final class AutoConfigurationReplacements {

	private static final String LOCATION = "META-INF/spring/%s.replacements";

	private final Map<String, String> replacements;

	private AutoConfigurationReplacements(Map<String, String> replacements) {
		this.replacements = Map.copyOf(replacements);
	}

	Set<String> replaceAll(Set<String> classNames) {
		Set<String> replaced = new LinkedHashSet<>(classNames.size());
		for (String className : classNames) {
			replaced.add(replace(className));
		}
		return replaced;
	}

	String replace(String className) {
		return this.replacements.getOrDefault(className, className);
	}

	static AutoConfigurationReplacements load(Class<?> annotation, @Nullable ClassLoader classLoader) {
		Assert.notNull(annotation, "'annotation' must not be null");
		ClassLoader classLoaderToUse = decideClassloader(classLoader);
        // META-INF/spring/%s.replacements
        // annotation 是 AutoConfiguration,全限定名是 org.springframework.boot.autoconfigure.AutoConfiguration
        // 格式化之后就是 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.replacements
		String location = String.format(LOCATION, annotation.getName());
        // 获取多个 jar 中的文件路径
		Enumeration<URL> urls = findUrlsInClasspath(classLoaderToUse, location);
		Map<String, String> replacements = new HashMap<>();
        // 循环读取 AutoConfiguration.replacements 文件
		while (urls.hasMoreElements()) {
			URL url = urls.nextElement();
            // readReplacements 使用 Properties.load 加载文件
			replacements.putAll(readReplacements(url));
		}
		return new AutoConfigurationReplacements(replacements);
	}

	private static ClassLoader decideClassloader(@Nullable ClassLoader classLoader) {
		if (classLoader == null) {
			return ImportCandidates.class.getClassLoader();
		}
		return classLoader;
	}

	private static Enumeration<URL> findUrlsInClasspath(ClassLoader classLoader, String location) {
		try {
			return classLoader.getResources(location);
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Failed to load configurations from location [" + location + "]", ex);
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private static Map<String, String> readReplacements(URL url) {
		try (BufferedReader reader = new BufferedReader(
				new InputStreamReader(new UrlResource(url).getInputStream(), StandardCharsets.UTF_8))) {
			Properties properties = new Properties();
            // 加载 .replacements 文件
			properties.load(reader);
			return (Map) properties;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load replacements from location [" + url + "]", ex);
		}
	}
}

参考

Creating Your Own Auto-configuration :: Deprecating and Replacing Auto-configuration Classes

相关推荐
许彰午12 小时前
13-describe元数据输出
java·低代码·架构·状态模式
李昊哲小课12 小时前
SpringBoot4 云端咖啡站 阶段二:数据访问与分层架构
spring boot·架构
用户37215742613513 小时前
Java 设置 Word 文档编辑限制并允许指定区域编辑
java
宠友信息14 小时前
社区类源码开发实践中的仿小红书系统技术要点分析
java·spring boot·redis·mysql·uni-app·vue·内容运营
用户37215742613514 小时前
Java 设置 PDF 表单域只读或扁平化
java
摇滚侠14 小时前
《SpringBoot 3:入门与应用实战》第 9 章 使用 WebMvc 开发应用 阅读笔记 1
spring boot·笔记·后端
李昊哲小课15 小时前
SpringBoot4 云端咖啡站 阶段四:安全、文件与性能
spring boot·安全·性能优化·文件·性能
luteres15 小时前
Spring学习笔记
java·后端·spring
南城以南溫暖如初14715 小时前
从零搭建24小时自助健身系统:技术选型与核心模块实战
java·spring boot·redis·mysql·vue·mybatis
重生之后端学习15 小时前
53. 最大子数组和[中等]✅
java·数据结构·算法·leetcode·职场和发展