Springboot - 打印多个yml最终合并配置信息

这篇具有很好参考价值的文章主要介绍了Springboot - 打印多个yml最终合并配置信息。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Springboot项目中多个yml配置优先级和最终配置容易混淆,本文帮助打出yml优先级和最终配置,以yml格式打印到控制台,便于开发调试。

一、服务启动后打印

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
@Component
public class ConfigurationPrinter implements ApplicationRunner {

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    public void printMergedConfiguration() {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        StringBuffer yamlFileNames= new StringBuffer("bootstrap.yml\n");
        environment.getPropertySources().stream().forEach(propertySource -> {
            if(propertySource.getName().contains("bootstrapProperties")){
                yamlFileNames.append(StringUtils.substringAfter(propertySource.getName(),"bootstrapProperties-")+"\n");
            }
        });
        log.info("\n************** Configuration files priority **************\n" + yamlFileNames.toString());
        ConfigurationPropertySources.attach(environment);
        Binder binder = Binder.get(environment);
        Map<String, Object> properties = binder.bind("", Map.class).orElse(null);
        properties.remove("java");
        properties.remove("sun");
        properties.remove("jboss");
        properties.remove("catalina");
        properties.entrySet().stream().sorted(Map.Entry.comparingByKey());
        Map<String, Object> sortedProperties = new LinkedHashMap<>();
        properties.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByKey())).forEachOrdered(x -> sortedProperties.put(x.getKey(), x.getValue()));
        printYaml(JsonUtils.toString(sortedProperties));
    }

    @Override
    public void run(ApplicationArguments args) {
        printMergedConfiguration();
    }

    private void printYaml(String json){
        try {
            ObjectMapper jsonMapper = new ObjectMapper();
            Object jsonObject = jsonMapper.readValue(json, Object.class);

            ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
            String yaml = yamlMapper.writeValueAsString(jsonObject);
            log.info("\n************** All configs **************\n" + yaml);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

二、监听事件打印文章来源地址https://www.toymoban.com/news/detail-504751.html

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class ConigurationPrinterEvent implements ApplicationListener<SpringApplicationEvent> {

    private static int printCount = 0;

    private final ConfigurableApplicationContext applicationContext;

    @Autowired
    public ConigurationPrinterEvent(ConfigurableApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void onApplicationEvent(SpringApplicationEvent event) {
        if (printCount == 0) {
            Environment environment = applicationContext.getEnvironment();
            ConfigurationPropertySources.attach(environment);
            Binder binder = Binder.get(environment);
            Map<String, Object> properties = binder.bind("", Map.class).orElse(null);
            printYaml(JsonUtils.toString(properties));
        }
    }

    private void printYaml(String json) {
        try {
            ObjectMapper jsonMapper = new ObjectMapper();
            Object jsonObject = jsonMapper.readValue(json, Object.class);

            ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
            String yaml = yamlMapper.writeValueAsString(jsonObject);
            log.info("\n************** Print all configs **************\n" + yaml);
            printCount++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

到了这里,关于Springboot - 打印多个yml最终合并配置信息的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • @ConfigurationProperties注解获取yml配置文件信息

    ConfigurationProperties注解主要用于将外部配置文件配置的属性填充到这个** Spring Bean实例 **中。 需要注意:它自己单独使用无效,需要配合其它注解一起使用。且对于Spring Bean才生效,普通的new 对象不生效。 ConfigurationProperties的使用方式: @ConfigurationProperties + @Component(或其它实

    2024年02月16日
    浏览(35)
  • java springboot yml文件配置 多环境yml

    如果是properties改用yml,直接新增一个 .yml ,删除原 .properties ,系统会自动扫描 application.properties 和 application.yml文件(如果同时存在两个文件,则会优先使用.properties文件?)。 注意:改了之后 需要maven 命令 clean一下 ,清个缓存。 一、yml多环境 如果需要配置多环境的配置

    2024年02月15日
    浏览(32)
  • Spring Boot读取yml或者properties配置信息

    编写配置类 开始使用

    2024年02月14日
    浏览(44)
  • SpringBoot案例-配置文件-yml配置文件

    SpringBoot提供了多种属性配置方式 application.properties application.yml application.yaml 常见配置文件格式对比 XML(臃肿) properties(层级结构不够清晰) yml/yaml( 简洁,以数据为中心, 推荐使用 ) 基本语法 大小写敏感 数值前必须有空格 ,作为分隔符 使用缩进表示层级关系,缩进时

    2024年02月11日
    浏览(33)
  • 【git 使用】使用 git rebase -i 修改任意的提交信息/合并多个提交

    修改最近一次的提交信息的方法有很多,可以参考这篇文章,但是对于之前的提交信息进行修改只能使用 rebase。 假设我们想修改下面这个提交信息,想把【登录】改成【退出登录】步骤如下 运行 git rebase -i head~3 打开了一个文本编辑器 -i  【interactive】参数表示进行交互式

    2024年02月21日
    浏览(31)
  • SpringBoot 常用的配置文件 application.yml和 bootstrap.yml的区别

    SpringBoot默认支持properties和YAML两种格式的配置文件。前者格式简单,但是只支持键值对。如果需要表达列表,最好使用YAML格式。SpringBoot支持自动加载约定名称的配置文件,例如application.yml。如果是自定义名称的配置文件,就要另找方法了。可惜的是,不像前者有@PropertySour

    2023年04月15日
    浏览(40)
  • SpringBoot配置文件application.yml的理解

    一、存放位置分类 1.当前项目根目录下的config目录下 2.当前项目的根目录下 3.resources目录下的config目录下 4.resources目录下 按照这上面的顺序,4个配置文件的优先级依次降低。  二、自定义存放位置和自定义命名 自定义存放位置和自定义配置文件命令和application.properties配置类

    2024年02月09日
    浏览(41)
  • SpringBoot的配置文件(properties与yml)

    SpringBoot 项目中所有重要的数据都是在配置文件中配置的,比如∶ 数据库的连接信息(包含用户名和密码的设置); 项目的启动端口; 第三方系统的调用秘钥等信息; 用于发现和定位问题的普通日志和异常日志; 还可以自定义配置,如配置上传文件的保存地址信息等。 想

    2024年02月12日
    浏览(30)
  • 17、YML配置文件及让springboot启动时加载我们自定义的yml配置文件的几种方式

    其实本质和.properties文件的是一样的。 Spring Boot默认使用SnakeYml工具来处理YAML配置文件,SnakeYml工具默认就会被spring-boot-starter导入,因此无需开发者做任何额外配置。 YAML本质是JSON的超级,它在表示结构化文档时更有表现力。 ▲ properties文件使用 .分隔符 作为结构化的表现:

    2024年02月14日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包