springboot配置文件加载

关键类:

org.springframework.boot.context.config.ConfigFileApplicationListener

最终加载文件的方法在其内部类Loader的load方法:

private void load(PropertySourceLoader loader, String location, Profile profile,DocumentFilter filter, DocumentConsumer consumer)

下面是我改写的方法,大家把这个方法替换掉,运行一遍即可知道springboot会去加载哪些配置文件

private void load(PropertySourceLoader loader, String location, Profile profile,
				DocumentFilter filter, DocumentConsumer consumer) {
			try {
				System.out.println("开始尝试加载配置文件:"+location+ ":::"+resourceLoader.getClass().getName());
				Resource resource = this.resourceLoader.getResource(location);
				if(resource instanceof org.springframework.core.io.FileUrlResource) {
					File file = resource.getFile();
					ResourceUtils.getFile(resource.getURL(), resource.getDescription());
				}
				
				if (resource == null || !resource.exists()) {
					
					try {
						System.out.println("不存在的配置文件"+resource.getFile().getAbsolutePath());
					}catch(Exception e) {
						System.out.println(e.getMessage());
					}
					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;
				}
				
				String name = "applicationConfig: [" + location + "]";
				List<Document> documents = loadDocuments(loader, name, resource);
				if (CollectionUtils.isEmpty(documents)) {
					try {
						System.out.println("配置文件内容为空"+resource.getFile().getAbsolutePath());
					}catch(Exception e) {
						System.out.println(e.getMessage());
					}
					if (this.logger.isTraceEnabled()) {
						StringBuilder description = getDescription(
								"Skipped unloaded config ", location, resource, profile);
						this.logger.trace(description);
					}
					return;
				}
				List<Document> loaded = new ArrayList<>();
				for (Document document : documents) {
					if (filter.match(document)) {
						addActiveProfiles(document.getActiveProfiles());
						addIncludedProfiles(document.getIncludeProfiles());
						loaded.add(document);
					}
				}
				Collections.reverse(loaded);
				if (!loaded.isEmpty()) {
					loaded.forEach((document) -> consumer.accept(profile, document));
					Resource resource1 = this.resourceLoader.getResource(location);
					
					try {
						System.out.println("######################成功加载文件"+resource.getFile().getAbsolutePath()+":::"+resource1.getClass().getName());
					}catch(Exception e) {
						System.out.println(e.getMessage());
					}
					if (this.logger.isDebugEnabled()) {
						StringBuilder description = getDescription("Loaded config file ",
								location, resource, profile);
						this.logger.debug(description);
					}
				}
			}
			catch (Exception ex) {
				System.out.println("加载配置文件异常");
				throw new IllegalStateException("Failed to load property "
						+ "source from location '" + location + "'", ex);
			}finally {
				System.out.println("加载完毕-----------------------------");
			}
		}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值