spring boot application.properties加载

本文详细解析了Spring Boot中ConfigFileApplicationListener类如何加载和解析application配置文件的过程,包括搜索路径确定、后缀名获取及数据读取等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述首先说明的是ConfigFileApplicationListener.java是加载解析处理application的地方.上面的图是非常省略的图,只画了个大概的流程.下面来仔细的说明说下
1.最开始的起来在SpringApplication里面在这里插入图片描述这里是从spring.factories里面读取配置,而我们所需要的类ConfigFileApplicationListeneryi就是从这里读出来的,也就是起始地方.看下具体数据在这里插入图片描述
这个类被读取出来后,在适当的地方会被加载到beanfactory的Listener里面,具体的地方看下面.在这里插入图片描述在这里插入图片描述这里调用addApplicationListener注册到工厂里面,也就是AbstractApplicationContext的addApplicationListener接口.代码自行去看.
2在refresh()初始始容器的时候有这么个函数调用.在这里插入图片描述在这里插入图片描述这里就是将我们先前注册的对象,取出来...下面细节不说了,直接到具体的处理函数里面吧
postProcessEnvironment->addPropertySources-.>Loader->load
1.经过一些操作之后,最终会触发ConfigFileApplicationListener的函数postProcessEnvironment的调用,最后走到最后实现的地方load操作.这里对这个函数进行深入分析.

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;
		}
	//上面的这段函数,主要是给出搜索路径,路径如下
		public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = **"spring.config.additional-location";**
			private static final String DEFAULT_SEARCH_LOCATIONS = **"classpath:/,classpath:/config/,file:./,file:./config/";** 这里面以,号进行切割

		private void load(Profile profile, DocumentFilterFactory filterFactory, DocumentConsumer consumer) {
			getSearchLocations().forEach((location) -> {
				boolean isFolder = location.endsWith("/");
				Set<String> names = isFolder ? getSearchNames() : NO_SEARCH_NAMES;
				//这里对所有的搜索路径进行搜索.getSearchNames返回是的application这个值
				names.forEach((name) -> load(location, name, profile, filterFactory, consumer));
			});
		}

		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;
					}
				}
			}
			//下面这段是重点,看图好说点.
			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);
					}
				}
			}
		}

在这里插入图片描述
1.我们知道要找以application开头的配置文件,但后缀名没给出来,所以下面就是要得到后缀名了
2看propertySourceLoaders这个初始化,以及getFileExtensions函数调用.首先来看init在这里插入图片描述好了,这里也是读取配置文件.看里面给出的是什么类在这里插入图片描述
这里面给了两个类,分别看下这两个类的getFileExtensions实现.
第一个类在这里插入图片描述
第二个类在这里插入图片描述可以看出后缀分别是properties", “xml”,“yml”,“yaml”
前缀有了,后缀有了就可以开始读取数据了.如下
loadForFileExtension(loader, location + name, “.” + fileExtension, profile, filterFactory…
location + name, “.” + fileExtension这里进行字符串拼接啦.难道直接搜怎么都搜不到

好了,就到这里了.后面就是各自调用自己的load函数来加载解析数据了.具体的细节自己看啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bruk_spp

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值