SpringBoot 扩展篇:ConfigFileApplicationListener源码解析

SpringBoot 扩展篇:ConfigFileApplicationListener源码解析

    • 1.概述
    • 2. ConfigFileApplicationListener定义
    • 3. ConfigFileApplicationListener回调链路
      • 3.1 SpringApplication#run
      • 3.2 SpringApplication#prepareEnvironment
      • 3.3 配置environment
    • 4. 环境准备事件 ConfigFileApplicationListener#onApplicationEvent
    • 4. 加载配置类
      • 4.1 Loader相关属性介绍
      • 4.2 Loader加载配置文件
        • FilteredPropertySource#apply
        • ConfigFileApplicationListener.Loader#load()
        • ConfigFileApplicationListener.Loader#initializeProfiles
        • Loader#addLoadedPropertySources
        • ConfigFileApplicationListener.Loader#getSearchLocations()
        • ConfigFileApplicationListener.Loader#load()
        • ConfigFileApplicationListener.Loader#load()
        • ConfigFileApplicationListener.Loader#loadForFileExtension
        • ConfigFileApplicationListener.Loader#load
      • 配置文件加载顺序总结
      • 问题:为什么先加入到environment中的propertySource,优先级越高?
      • 遗留问题:

1.概述

SpringBoot的配置文件加载由ConfigFileApplicationListener完成的,它会加载application.properties、application.yml等配置文件,还支持用户配置和扩展。本文从源码的角度分析它的原理

加载完毕的配置信息最终都会放入到Environment中。

2. ConfigFileApplicationListener定义

在这里插入图片描述
ConfigFileApplicationListener定义在spring.factories中。监听器注册和执行原理参考:SpringBoot 源码解析3:事件监听器

3. ConfigFileApplicationListener回调链路

3.1 SpringApplication#run

public ConfigurableApplicationContext run(String... args) {
	StopWatch stopWatch = new StopWatch();
	stopWatch.start();
	ConfigurableApplicationContext context = null;
	configureHeadlessProperty();
	SpringApplicationRunListeners listeners = getRunListeners(args);
	listeners.starting();
	try {
		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
		ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
		configureIgnoreBeanInfo(environment);
		Banner printedBanner = printBanner(environment);
		context = createApplicationContext();
		prepareContext(context, environment, listeners, applicationArguments, printedBanner);
		refreshContext(context);
		afterRefresh(context, applicationArguments);
		stopWatch.stop();
		if (this.logStartupInfo) {
			new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
		}
		listeners.started(context);
		callRunners(context, applicationArguments);
	}
	catch (Throwable ex) {
		handleRunFailure(context, ex, listeners);
		throw new IllegalStateException(ex);
	}

	try {
		listeners.running(context);
	}
	catch (Throwable ex) {
		handleRunFailure(context, ex, null);
		throw new IllegalStateException(ex);
	}
	return context;
}

这是SpringBoot启动最基础的方法,调用了prepareEnvironment。

3.2 SpringApplication#prepareEnvironment

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
		ApplicationArguments applicationArguments) {
	// Create and configure the environment
	// 创建environment对象
	ConfigurableEnvironment environment = getOrCreateEnvironment();
	// 配置环境
	configureEnvironment(environment, applicationArguments.getSourceArgs());
	ConfigurationPropertySources.attach(environment);
	// 发布监听事件
	listeners.environmentPrepared(environment);
	bindToSpringApplication(environment);
	if (!this.isCustomEnvironment) {
		environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
				deduceEnvironmentClass());
	}
	ConfigurationPropertySources.attach(environment);
	return environment;
}
  1. getOrCreateEnvironment创建StandardServletEnvironment,所有的启动参数和配置文件信息都会保存到environment中。environment中默认创建了4个propertySource,分别用来存放系统属性和servlet属性。
    在这里插入图片描述

  2. configureEnvironment配置环境信息,此时配置文件还没解析。

  3. listeners.environmentPrepared,调用监听器ConfigFileApplicationListener解析配置文件。最终回调了ConfigFileApplicationListener#onApplicationEvent,这里是解析文件的核心逻辑。

@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
	this.initialMulticaster
			.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));
}

监听器发布的是ApplicationEnvironmentPreparedEvent类型的事件。

  1. bindToSpringApplication解析完毕所有的配置文件信息之后,将spring.main.*的环境变量与当前的springApplication对象的属性绑定。比如allowBeanDefinitionOverriding配置就是在这里读取的。

3.3 配置environment

SpringApplication#configureEnvironment

protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
	if (this.addConversionService) {
		ConversionService conversionService = ApplicationConversionService.getSharedInstance();
		environment.setConversionService((ConfigurableConversionService) conversionService);
	}
	configurePropertySources(environment, args);
	configureProfiles(environment, args);
}
  1. 第二个参数args为SpringBoot启动参数。
  2. configurePropertySources方法会将启动参数解析保存到environment中。
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
	MutablePropertySources sources = environment.getPropertySources();
	if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
		sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
	}
	if (this.addCommandLineProperties && args.length > 0) {
		String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
		if (sources.contains(name)) {
			PropertySource<?> source = sources.get(name);
			CompositePropertySource composite = new CompositePropertySource(name);
			composite.addPropertySource(
					new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
			composite.addPropertySource(source);
			sources.replace(name, composite);
		}
		else {
			sources.addFirst(new SimpleCommandLinePropertySource(args));
		}
	}
}

通过addFirst会将启动参数的属性添加到第一个PropertySources,优先级最高

protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
	if (this.propertySources != null) {
		for (PropertySource<?> propertySource : this.propertySources) {
			if (logger.isTraceEnabled()) {
				logger.trace("Searching for key '" + key + "' in PropertySource '" +
						propertySource.getName() + "'");
			}
			Object value = propertySource.getProperty(key);
			if (value != null) {
				if (resolveNestedPlaceholders && value instanceof String) {
					value = resolveNestedPlaceholders((String) value);
				}
				logKeyFound(key, propertySource, value);
				return convertValueIfNecessary(value, targetValueType);
			}
		}
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Could not find key '" + key + "' in any property source");
	}
	return null;
}

如果有多个相同的key在不同的propertySource中,在通过key从environment中获取值的时候,会遍历所有的PropertySources,获取到第一个就会返回。
3. configureProfiles方法

protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {
	Set<String> profiles = new LinkedHashSet<>(this.additionalProfiles);
	profiles.addAll(Arrays.asList(environment.getActiveProfiles()));
	environment.setActiveProfiles(StringUtils.toStringArray(profiles));
}

通过environment.getActiveProfiles() 获取spring.profiles.active的值,此时的配置文件还没有解析,获取到的是启动参数中的值。

4. 环境准备事件 ConfigFileApplicationListener#onApplicationEvent

由上文可知,发布的是ApplicationEnvironmentPreparedEvent类型的事件

@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof ApplicationEnvironmentPreparedEvent) {
		onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
	}
	if (event instanceof ApplicationPreparedEvent) {
		onApplicationPreparedEvent(event);
	}
}

private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
	List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
	postProcessors.add(this);
	AnnotationAwareOrderComparator.sort(postProcessors);
	for (EnvironmentPostProcessor postProcessor : postProcessors) {
		postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
	}
}

loadPostProcessors会从spring.factories中加载所有EnvironmentPostProcessor类型的处理器。
SpringBoot 基础概念:SpringApplication#getSpringFactoriesInstances
最终将自己加入到这些处理器中,然后依次执行postProcessEnvironment方法。

4. 加载配置类

加载配置类的核心逻辑的入口在 ConfigFileApplicationListener#postProcessEnvironment。

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
	addPropertySources(environment, application.getResourceLoader());
}
protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
	RandomValuePropertySource.addToEnvironment(environment);
	new Loader(environment, resourceLoader).load();
}

可以看到,加载配置的逻辑交给了Loader。

4.1 Loader相关属性介绍

Loader是ConfigFileApplicationListener的内部类。

构造器

Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
	this.environment = environment;
	this.placeholdersResolver = new PropertySourcesPlaceholdersResolver(this.environment);
	this.resourceLoader = (resourceLoader != null) ? resourceLoader : new DefaultResourceLoader();
	this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class,
			getClass().getClassLoader());
}

propertySourceLoaders 是从spring.factories文件中加载的配置文件加载器。
在这里插入图片描述
PropertiesPropertySourceLoader负责读取*.properties、*.xml中的内容

public class PropertiesPropertySourceLoader implements PropertySourceLoader {

	private static final String XML_FILE_EXTENSION = ".xml";

	@Override
	public String[] getFileExtensions() {
		return new String[] { "properties", "xml" };
	}
	....
}

YamlPropertySourceLoader负责读取*.yml、*.yaml文件中的内容

public class YamlPropertySourceLoader implements PropertySourceLoader {

	@Override
	public String[] getFileExtensions() {
		return new String[] { "yml", "yaml" };
	}
}

ConfigFileApplicationListener中的属性

private static final String DEFAULT_PROPERTIES = "defaultProperties";

// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";

private static final String DEFAULT_NAMES = "application";

private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null);

private static final Bindable<String[]> STRING_ARRAY = Bindable.of(String[].class);

private static final Bindable<List<String>> STRING_LIST = Bindable.listOf(String.class);

private static final Set<String> LOAD_FILTERED_PROPERTY;

static {
	Set<String> filteredProperties = new HashSet<>();
	filteredProperties.add("spring.profiles.active");
	filteredProperties.add("spring.profiles.include");
	LOAD_FILTERED_PROPERTY = Collections.unmodifiableSet(filteredProperties);
}

Loader类中的属性

private final Log logger = ConfigFileApplicationListener.this.logger;

// environment,Spring所有解析的配置信息和启动参数都放入到了environment中 
private final ConfigurableEnvironment environment;

// 占位符解析器,解析${key}
private final PropertySourcesPlaceholdersResolver placeholdersResolver;

// 资源加载器,加载文件资源
private final ResourceLoader resourceLoader;

private final List<PropertySourceLoader> propertySourceLoaders;

// profile对应spring.profiles.active、spring.profiles.include、spring.profiles.default对应的配置属性
private Deque<Profile> profiles;

private List<Profile> processedProfiles;

private boolean activatedProfiles;

private Map<Profile, MutablePropertySources> loaded;

private Map<DocumentsCacheKey, List<Document>> loadDocumentsCache = new HashMap<>();

4.2 Loader加载配置文件

FilteredPropertySource#apply
static void apply(ConfigurableEnvironment environment, String propertySourceName, Set<String> filteredProperties,
		Consumer<PropertySource<?>> operation) {
	MutablePropertySources propertySources = environment.getPropertySources();
	PropertySource<?> original = propertySources.get(propertySourceName);
	// 判断environment中是否有名称为"defaultProperties"的资源
	if (original == null) {
		// 如果没有defaultProperties资源,那么就回调Loader类中的Consumer方法
		operation.accept(null);
		return;
	}
	// 如果有defaultProperties资源,就封装成FilteredPropertySource
	propertySources.replace(propertySourceName, new FilteredPropertySource(original, filteredProperties));
	try {
		// 回调Loader类中的Consumer方法
		operation.accept(original);
	}
	finally {
		// 替换PropertySource
		propertySources.replace(propertySourceName, original);
	}
}

如果environment中没有名称为“defaultProperties”属性资源,那么就直接回调Loader中的Consumer方法 (defaultProperties) -> { … } ,参数为null。

Springboot默认是没有defaultProperties的
在这里插入图片描述

ConfigFileApplicationListener.Loader#load()

对加载配置文件时所需的属性初始化。

void load() {
	FilteredPropertySource.apply(this.environment, DEFAULT_PROPERTIES, LOAD_FILTERED_PROPERTY,
			(defaultProperties) -> {
				this.profiles = new LinkedList<>();
				this.processedProfiles = new LinkedList<>();
				this.activatedProfiles = false;
				this.loaded = new LinkedHashMap<>();
				// 初始化profile
				initializeProfiles();
				while (!this.profiles.isEmpty()) {
					Profile profile = this.profiles.poll();
					if (isDefaultProfile(profile)) {
						addProfileToEnvironment(profile.getName());
					}
					// 加载配置文件
					load(profile, this::getPositiveProfileFilter,
							addToLoaded(MutablePropertySources::addLast, false));
					this.processedProfiles.add(profile);
				}
				load(null, this::getNegativeProfileFilter, addToLoaded(MutablePropertySources::addFirst, true));
				// 将加载到的PropertySources放入到environment的最后
				addLoadedPropertySources();
				// 将所有加载到了的profiles,设置到environment中
				applyActiveProfiles(defaultProperties);
			});
}
  1. (defaultProperties) -> { … } 是一个函数接口 Consumer,所以需要先看 FilteredPropertySource#apply方法,在apply方法内部回调这个Consumer。
  2. initializeProfiles:初始化profile。
  3. profiles.poll先入先出,依次加载profile,后续的配置文件中有profile,也会放入到profiles中。
  4. addLoadedPropertySources方法,在profiles循环完毕,所有配置加载完毕,将读取到的内容添加到environment中。
ConfigFileApplicationListener.Loader#initializeProfiles

初始化profile。日常工作中profile指的是dev、uat、prod等配置,但是我们的思维不要局限于这里。

private void initializeProfiles() {
	// The default profile for these purposes is represented as null. We add it
	// first so that it is processed first and has lowest priority.
	// 1. 添加一个为null的profile
	this.profiles.add(null);
	// 获取spring.profiles.active
	Set<Profile> activatedViaProperty = getProfilesFromProperty(ACTIVE_PROFILES_PROPERTY);
	// 获取spring.profiles.include
	Set<Profile> includedViaProperty = getProfilesFromProperty(INCLUDE_PROFILES_PROPERTY);
	// 获取不在当前spring.profiles.active和spring.profiles.include范围内,并且之前获取到的spring.profiles.active(环境)
	List<Profile> otherActiveProfiles = getOtherActiveProfiles(activatedViaProperty, includedViaProperty);
	// 2. 添加不在当前spring.profiles.active和spring.profiles.include范围内的,之前获取到的spring.profiles.active
	this.profiles.addAll(otherActiveProfiles);
	// Any pre-existing active profiles set via property sources (e.g.
	// System properties) take precedence over those added in config files.
	// 3. 添加spring.profiles.include对应的profile
	this.profiles.addAll(includedViaProperty);
	// 4. 添加spring.profiles.active对应的profile
	addActiveProfiles(activatedViaProperty);
	// 5. 如果没有spring.profiles.active和spring.profiles.include,那么就使用spring.profiles.default
	if (this.profiles.size() == 1) { // only has null profile
		for (String defaultProfileName : this.environment.getDefaultProfiles()) {
			Profile defaultProfile = new Profile(defaultProfileName, true);
			this.profiles.add(defaultProfile);
		}
	}
}

profiles添加的优先顺序,决定了profile加载的顺序,先进先出

  1. 添加一个为null的profile。因为就算用户配置了spring.profiles.active=dev,不仅要加载application-dev.yml文件,application.yml文件也需要被加载。
  2. 添加不在当前spring.profiles.active和spring.profiles.include范围内的,之前获取到的spring.profiles.active。前期在SpringBoot启动的时候在SpringApplication#configureProfiles方法中就已经获取到了启动参数中的spring.profiles.active。
  3. 添加spring.profiles.include对应的profile。
  4. 添加spring.profiles.active对应的profile。
  5. 如果没有spring.profiles.active和spring.profiles.include,那么就使用spring.profiles.default对应的profile。
Loader#addLoadedPropertySources

将配置文件中加载的属性放入到environment中。

private void addLoadedPropertySources() {
	MutablePropertySources destination = this.environment.getPropertySources();
	List<MutablePropertySources> loaded = new ArrayList<>(this.loaded.values());
	Collections.reverse(loaded);
	String lastAdded = null;
	Set<String> added = new HashSet<>();
	for (MutablePropertySources sources : loaded) {
		for (PropertySource<?> source : sources) {
			if (added.add(source.getName())) {
				addLoadedPropertySource(destination, lastAdded, source);
				lastAdded = source.getName();
			}
		}
	}
}

loaded存放的是已经加载过的属性,它是一个LinkedHashMap,key为profile,value为propertySource,一个propertySource对应一个配置文件。会将profile加载的顺序颠倒,通过addLoadedPropertySource添加到environment中。在environment中,先加入的property,优先级越高
所以,后加载的profile,优先级就越高。这也就是为什么上述Loader#initializeProfiles方法中this.profiles.add(null)这行代码的意义,就是为了将没有profile的文件的优先级降到最低。

ConfigFileApplicationListener.Loader#getSearchLocations()
private Set<String> getSearchLocations() {
	if (this.environment.containsProperty(CONFIG_LOCATION_PROPERTY)) {
		return getSearchLocations(CONFIG_LOCATION_PROPERTY);
	}
	Set<String> locations = getSearchLocations(CONFIG_ADDITIONAL_LOCATION_PROPERTY);
	locations.addAll(
			asResolvedSet(ConfigFileApplicationListener.this.searchLocations, DEFAULT_SEARCH_LOCATIONS));
	return locations;
}

获取文件父路径

  1. spring.config.location强制指定文件路径,只能从这个路径下面寻找文件
  2. spring.config.additional-location额外的文件查找路径
  3. 默认了四个路径:classpath:/,classpath:/config/,file:./,file:./config/,在asResolvedSet方法中将顺序颠倒了。
  4. locations中可配置文件父路径,也可能是文件的绝对路径。
ConfigFileApplicationListener.Loader#load()
private void load(Profile profile, DocumentFilterFactory filterFactory, DocumentConsumer consumer) {
	getSearchLocations().forEach((location) -> {
		boolean isFolder = location.endsWith("/");
		Set<String> names = isFolder ? getSearchNames() : NO_SEARCH_NAMES;
		names.forEach((name) -> load(location, name, profile, filterFactory, consumer));
	});
}
  1. 先遍历所有的locations,如果location为文件,那么就直接通过location加载配置。如果为文件夹,那么就查询获取文件名称,通过文件夹+文件名称去加载文件。
  2. getSearchNames() : 获取文件名称,可通过spring.config.name配置文件名称,没有配置则使用"application"。这也解释了SpringBoot启动的时候,为什么回去加载application.yml文件。
private Set<String> getSearchNames() {
	if (this.environment.containsProperty(CONFIG_NAME_PROPERTY)) {
		String property = this.environment.getProperty(CONFIG_NAME_PROPERTY);
		return asResolvedSet(property, null);
	}
	return asResolvedSet(ConfigFileApplicationListener.this.names, DEFAULT_NAMES);
}
ConfigFileApplicationListener.Loader#load()
private void load(String location, String name, Profile profile, DocumentFilterFactory filterFactory,
		DocumentConsumer consumer) {
	if (!StringUtils.hasText(name)) {
		for (PropertySourceLoader loader : this.propertySourceLoaders) {
			if (canLoadFileExtension(loader, location)) {
				load(loader, location, profile, filterFactory.getDocumentFilter(profile), consumer);
				return;
			}
		}
		throw new IllegalStateException("File extension of config file location '" + location
				+ "' is not known to any PropertySourceLoader. If the location is meant to reference "
				+ "a directory, it must end in '/'");
	}
	Set<String> processed = new HashSet<>();
	for (PropertySourceLoader loader : this.propertySourceLoaders) {
		for (String fileExtension : loader.getFileExtensions()) {
			if (processed.add(fileExtension)) {
				loadForFileExtension(loader, location + name, "." + fileExtension, profile, filterFactory,
						consumer);
			}
		}
	}
}

location可能为文件的全路径(spring.config.additional-location配置),为全路径则文件名称name为null。分成了两种方式加载文件,其实两种方式的逻辑是一样的。我们关注下半部分location + name, “.” + fileExtension加载方式。
在这里插入图片描述
先使用Properties加载器,在使用Yaml加载器。这就是为什么properties文件优先级高于yaml文件的原因。

ConfigFileApplicationListener.Loader#loadForFileExtension

通过文件的扩展名称加载

private void loadForFileExtension(PropertySourceLoader loader, String prefix, String fileExtension,
		Profile profile, DocumentFilterFactory filterFactory, DocumentConsumer consumer) {
	DocumentFilter defaultFilter = filterFactory.getDocumentFilter(null);
	DocumentFilter profileFilter = filterFactory.getDocumentFilter(profile);
	if (profile != null) {
		// Try profile-specific file & profile section in profile file (gh-340)
		String profileSpecificFile = prefix + "-" + profile + fileExtension;
		load(loader, profileSpecificFile, profile, defaultFilter, consumer);
		load(loader, profileSpecificFile, profile, profileFilter, consumer);
		// Try profile specific sections in files we've already processed
		for (Profile processedProfile : this.processedProfiles) {
			if (processedProfile != null) {
				String previouslyLoaded = prefix + "-" + processedProfile + fileExtension;
				load(loader, previouslyLoaded, profile, profileFilter, consumer);
			}
		}
	}
	// Also try the profile-specific section (if any) of the normal file
	load(loader, prefix + fileExtension, profile, profileFilter, consumer);
}
  1. prefix为文件夹路径+文件名称,比如:file:./config/application
  2. 如果profile不为空,那么就使用prefix + “-” + profile + fileExtension加载,比如:file:./config/application-dev.yml。
  3. 最后,不管profile是否为空,都会通过prefix + fileExtension加载,比如:file:./config/application.yml。
ConfigFileApplicationListener.Loader#load
private void load(PropertySourceLoader loader, String location, Profile profile, DocumentFilter filter,
		DocumentConsumer consumer) {
	try {
		// 判断文件资源是否存在
		Resource resource = this.resourceLoader.getResource(location);
		if (resource == null || !resource.exists()) {
			if (this.logger.isTraceEnabled()) {
				StringBuilder description = getDescription("Skipped missing config ", location, resource,
						profile);
				this.logger.trace(description);
			}
			return;
		}
		// 校验文件扩展名称不为空
		if (!StringUtils.hasText(StringUtils.getFilenameExtension(resource.getFilename()))) {
			if (this.logger.isTraceEnabled()) {
				StringBuilder description = getDescription("Skipped empty config extension ", location,
						resource, profile);
				this.logger.trace(description);
			}
			return;
		}
		// 读取配置文件中配置,转换成Document
		String name = "applicationConfig: [" + location + "]";
		List<Document> documents = loadDocuments(loader, name, resource);
		if (CollectionUtils.isEmpty(documents)) {
			if (this.logger.isTraceEnabled()) {
				StringBuilder description = getDescription("Skipped unloaded config ", location, resource,
						profile);
				this.logger.trace(description);
			}
			return;
		}
		List<Document> loaded = new ArrayList<>();
		// 添加新的active和include的profile
		for (Document document : documents) {
			if (filter.match(document)) {
				addActiveProfiles(document.getActiveProfiles());
				addIncludedProfiles(document.getIncludeProfiles());
				loaded.add(document);
			}
		}
		// 将此次加载的Document顺序颠倒
		Collections.reverse(loaded);
		if (!loaded.isEmpty()) {
			loaded.forEach((document) -> consumer.accept(profile, document));
			if (this.logger.isDebugEnabled()) {
				StringBuilder description = getDescription("Loaded config file ", location, resource, profile);
				this.logger.debug(description);
			}
		}
	}
	catch (Exception ex) {
		throw new IllegalStateException("Failed to load property source from location '" + location + "'", ex);
	}
}
  1. 通过一系列的校验,比如文件资源、文件扩展名是否存在。当校验都通过了,才会去加载资源。
  2. loadDocuments方法读取配置文件中的信息,封装成了Document返回。虽然返回的是List,实际上List中只有一个元素,因为每次只会加载一个资源文件。可能是Spring为了扩展,而返回List吧。
private List<Document> loadDocuments(PropertySourceLoader loader, String name, Resource resource)
		throws IOException {
	DocumentsCacheKey cacheKey = new DocumentsCacheKey(loader, resource);
	List<Document> documents = this.loadDocumentsCache.get(cacheKey);
	if (documents == null) {
		List<PropertySource<?>> loaded = loader.load(name, resource);
		documents = asDocuments(loaded);
		this.loadDocumentsCache.put(cacheKey, documents);
	}
	return documents;
}

加载资源文件,根据文件的扩展名,回调了对应的PropertiesPropertySourceLoader#load、YamlPropertySourceLoader#load。

  1. resourceLoader.getResource获取文件资源支持通过URL获取。DefaultResourceLoader#getResource。
@Override
public Resource getResource(String location) {
	Assert.notNull(location, "Location must not be null");

	for (ProtocolResolver protocolResolver : getProtocolResolvers()) {
		Resource resource = protocolResolver.resolve(location, this);
		if (resource != null) {
			return resource;
		}
	}

	if (location.startsWith("/")) {
		return getResourceByPath(location);
	}
	else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
		return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
	}
	else {
		try {
			// Try to parse the location as a URL...
			URL url = new URL(location);
			return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
		}
		catch (MalformedURLException ex) {
			// No URL -> resolve as resource path.
			return getResourceByPath(location);
		}
	}
}
  1. asDocuments,将加载到的资源封装成Document
private List<Document> asDocuments(List<PropertySource<?>> loaded) {
	if (loaded == null) {
		return Collections.emptyList();
	}
	return loaded.stream().map((propertySource) -> {
		Binder binder = new Binder(ConfigurationPropertySources.from(propertySource),
				this.placeholdersResolver);
		return new Document(propertySource, binder.bind("spring.profiles", STRING_ARRAY).orElse(null),
				getProfiles(binder, ACTIVE_PROFILES_PROPERTY), getProfiles(binder, INCLUDE_PROFILES_PROPERTY));
	}).collect(Collectors.toList());
}

通过binder将propertySource中的spring.profiles.active和spring.profiles.include解析成String数组,分别绑定到了Document对象的activeProfiles和includeProfiles属性,以便后面使用。
6. 遍历所有的Document对象,实际上只有一个Document,因为文件资源只有一个。将Document中的activeProfiles和includeProfiles重新加入到profiles中。因为最外面第一层Loader#load()中正在遍历profiles,下次循环会重新加载后续的profile。
7. loaded.forEach((document) -> consumer.accept(profile, document));

配置文件加载顺序总结

  1. 遍历profile。假如启动脚本中有spring.profiles.active、spring.profiles.include的profile为dev,则遍历null、dev。现进先出,先加载为null的profile。越先加载的profile,优先级越低
  2. 遍历location。相关配置:spring.config.location、spring.config.additional-location,默认配置为classpath:/,classpath:/config/,file:./,file:./config/。注意:会先将location的顺序颠倒,再去加载。
  3. 遍历文件名称。相关配置spring.config.name。默认application。
  4. 判断文件名称是否为空。如果location不是文件夹(不以“/”结尾,那么就认为不是文件夹),则使用location去加载文件。否则就拼接文件名称加载。
  5. 遍历文件扩展名。先遍历资源加载器,每一个资源加载器都支持不同的文件扩展名。PropertiesPropertySourceLoader支持properties、xml,YamlPropertySourceLoader支持yml、yaml。
  6. 最终将加载到的所有信息放入到ConfigFileApplicationListener.Loader#loaded。loaded是LinkedHashMap,key为profile,value为对应的文件名称加载是所有资源propertySource。最终会颠倒profile加载的顺序,将propertySource放入到environment中。
  7. 放入environment的先后顺序决定了取配置的优先级,越先加入到environment中的propertySource,优先级越高。

问题:为什么先加入到environment中的propertySource,优先级越高?

environment#getProperty()获取属性key所对应的值。调用链路如下

AbstractEnvironment#getProperty(java.lang.String)

public String getProperty(String key) {
	return this.propertyResolver.getProperty(key);
}

org.springframework.core.env.PropertySourcesPropertyResolver#getProperty(java.lang.String)

public String getProperty(String key) {
	return getProperty(key, String.class, true);
}

PropertySourcesPropertyResolver#getProperty(java.lang.String, java.lang.Class, boolean)

protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
	if (this.propertySources != null) {
		for (PropertySource<?> propertySource : this.propertySources) {
			if (logger.isTraceEnabled()) {
				logger.trace("Searching for key '" + key + "' in PropertySource '" +
						propertySource.getName() + "'");
			}
			Object value = propertySource.getProperty(key);
			if (value != null) {
				if (resolveNestedPlaceholders && value instanceof String) {
					value = resolveNestedPlaceholders((String) value);
				}
				logKeyFound(key, propertySource, value);
				return convertValueIfNecessary(value, targetValueType);
			}
		}
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Could not find key '" + key + "' in any property source");
	}
	return null;
}

我们可以很清晰的看到,遍历了propertySources,从propertySource取到不为null的值。解析占位符、转换值类型之后,就返回了。

遗留问题:

  1. bootstrap.yml加载逻辑BootstrapApplicationListener。
  2. nacos中的配置文件如何加载到的?getSearchLocations中使用URL协议吗?nacos源码研究。

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

社交媒体数据恢复:抖音、火山版、极速版

抖音是一款非常受欢迎的短视频社交平台&#xff0c;在使用过程中&#xff0c;有时候我们会不小心删除了重要的聊天记录。那么&#xff0c;如何恢复抖音聊天记录呢&#xff1f;下面&#xff0c;我将为大家带来一份详细的抖音聊天记录数据恢复教程。 一、抖音聊天记录恢复方法 打…

Angular中组件之间的传值

Angular中组件之间的传值 文章目录 Angular中组件之间的传值前言一、父亲向儿子传值二、儿子向父亲传值三、爷爷向孙子传值四、兄弟之间的传值 前言 Angular的组件是构成应用的基础单元&#xff0c;它们封装了HTML模板、TypeScript代码以及CSS样式&#xff0c;以实现特定的功能…

【算法学习】day2

文章目录 BFS1.图像渲染2.岛屿数量 BFS 1.图像渲染 思路&#xff1a;BFS宽度遍历&#xff0c;我们需要对初始像素进行一层一层遍历&#xff0c;也就是上下左右四个方向进行遍历判断&#xff0c;如何访问这四个方向呢&#xff0c;就需要利用两个数组dx和dy来进行判断和遍历&…

【RPC】Dubbo接口测试

关于rpc&#xff0c;推荐看看这篇 &#xff1a; 既然有HTTP协议&#xff0c;为什么还要有RPC 一、Dubbo 是一款alibaba开源的高性能服务框架&#xff1a; 分布式服务框架高性能和透明化的RPC远程服务调用方案SOA服务治理方案 二、Dubbo基础架构 三、 Dubbo接口测试 1、jme…

毕业设计参考-PyQt5-YOLOv8-鱼头鱼尾鱼长测量程序,OpenCV、Modbus通信、YOLO目标检测综合应用

“PyQt5-YOLOv8-鱼头鱼尾鱼长测量程序”是一个特定的软件程序&#xff0c;用于通过图像处理和目标检测技术来测量鱼类的长度。 视频效果&#xff1a; 【毕业设计】基于yolo算法与传统机器视觉的鱼头鱼尾识别_哔哩哔哩_bilibili 这个程序结合了多种技术&#xff1a; 1. OpenCV…

并行执行的概念—— 《OceanBase 并行执行》系列 一

From 产品经理&#xff1a; 这是一份姗姗来迟的关于OceanBase并行执行的系统化产品文档。 自2019年起&#xff0c;并行执行功能已被许多客户应用于多种场景之中&#xff0c;其重要性日益凸显。然而&#xff0c;遗憾的是&#xff0c;我们始终未能提供一份详尽的用户使用文档&…

如何应对访问国外服务器缓慢的问题?SDWAN组网是性价比之选

访问国外服务器缓慢通常由以下原因造成&#xff1a; 1、政策限制&#xff1a;我国管理互联网&#xff0c;限制部分国外网站和服务器&#xff0c;以维护国家安全稳定。 2、技术障碍&#xff1a;国内与国际互联网的网络架构和协议存在差异&#xff0c;可能导致数据传输不兼容。 …

探索AI编程新纪元:从零开始的智能编程之旅

提示&#xff1a;Baidu Comate 智能编码助手是基于文心大模型&#xff0c;打造的新一代编码辅助工具 文章目录 前言AI编程概述&#xff1a;未来已来场景需求&#xff1a;从简单到复杂&#xff0c;无所不包体验步骤&#xff1a;我的AI编程初探试用感受&#xff1a;双刃剑下的深思…

docker资源限额

多数的应⽤场景要对Docker容器的运⾏内存进⾏限制&#xff0c;防⽌其使⽤过多的内存。 格式&#xff1a;-m或--memory 正常的内存大小 [rootadmin ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS …

“40法则”视角下的中国网络安全公司

“40法则”视角下国内网安上市公司2023年业绩表现 采用“40法则”衡量&#xff0c;首先需要考虑的是营收增长和利润水平的衡量指标&#xff0c;在上一篇文章中已经详细说明&#xff0c;在此不再赘述。 增长速度的衡量指标&#xff0c;可以选择公司的营业收入的同比增长率。 …

华为OD机试 - 掌握的单词个数 - 回溯(Java 2024 C卷 100分)

华为OD机试 2024C卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷C卷&#xff09;》。 刷的越多&#xff0c;抽中的概率越大&#xff0c;每一题都有详细的答题思路、详细的代码注释、样例测试…

怎么将pdf的文件内容保存到mysql数据库中?

要将PDF导入到MYSQL&#xff0c;首先一步就是要先将PDF内容结构化&#xff0c;如果其内容为非结构化&#xff0c;则导入MYSQL的意义不大&#xff0c;具体操作方法如下&#xff1a; 将PDF文件的内容保存到MySQL数据库中通常涉及几个步骤。PDF文件包含的是格式化文本、图像和其他…

​XMall商城微信小程序前端技术解析

摘要 随着移动互联网的深入发展&#xff0c;微信小程序以其轻量级、便捷性和即用即走的特点&#xff0c;成为了众多企业和开发者关注的焦点。XMall商城微信小程序前端作为一款开源项目&#xff0c;以其精美的页面设计、丰富的功能和高效的性能&#xff0c;受到了广大开发者和用…

深度学习之基于Matlab BP神经网络烟叶成熟度分类

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 一、项目背景 烟叶的成熟度是评估烟叶品质的重要指标之一&#xff0c;它直接影响着烟叶的口感、香气和理化特性。传…

还不懂 RESTful 接口是什么?快进来看看

RESTful是指基于REST&#xff08;Representational State Transfer&#xff0c;表现层状态转移&#xff09;架构风格的Web服务。REST是一种设计原则和架构风格&#xff0c;而不是标准&#xff0c;它用于指导如何构建易于交互、高效、可扩展的网络系统。RESTful服务通常使用HTTP…

Oracle Database 23ai Free RPM Installation On Oracle Linux 8 (OL8)

Oracle刚刚发布了最新的Oracle database 23ai版本测试安装包&#xff0c;有兴趣的小伙伴可以安装体验一下。 关于安装的介质可以去如下地址下载&#xff1a; Oracle linux 8.9 Oracle Linux ISOs | Oracle, Software. Hardware. Complete. Oracle database 23ai安装包 Get Star…

Read timed out. (python 安装第三方库超时)

不少人在安装python第三方库的时候经常发生下面情况 解决方法就是往上找 我这里就是 jupyterlab-4.1.8-py3-none-any.whl安装时间过长&#xff0c;失败 那就去国内镜像网站下载下来离线安装 https://pypi.tuna.tsinghua.edu.cn/simple/xxx&#xff08;xxx就是你的包名&#…

AI绘画Stable Diffusion【艺术写真】:冰雪奇缘,使用ReActor插件实现AI写真

大家好&#xff0c;我是设计师阿威。 前面分享过几篇使用AI绘画Stable DIffusion中的InstantID插件实现AI写真的制作方法。 目前换脸插件有很多&#xff0c;比较典型的有Roop,ReActor,IP-Adapter,InstantID&#xff0c;就目前效果来看&#xff0c;InstantID单张图像换脸的相似…

数据结构:时间复杂度/空间复杂度

目录 一、时间复杂度 定义 常见的时间复杂度 如何计算时间复杂度 计算方法 三、实例分析 二、空间复杂度 定义 重要性 常见的空间复杂度 二、空间复杂度 定义 重要性 常见的空间复杂度 计算方法 三、实例分析 大O的渐进表示法 最好情况&#xff08;Best Case…

【前端】实现表格简单操作

简言 表格合并基础篇 本篇是在上一章的基础上实现&#xff0c;实现了的功能有添加行、删除行、逆向选区、取消合并功能。 功能实现 添加行 添加行分为在上面添加和在下面追加行。 利用 insertAdjacentElement 方法实现&#xff0c;该方法可以实现从前插入元素和从后插入元…
最新文章