springboot读取application.yml文件

场景:工具类读取application.yml配置文件信息
记录:NO.237
本例环境
        spring-boot:2.2.0.RELEASE
        jdk:1.8
1.加载单个配置文件

public static Object getYmlPropertyValue(String key) {
  
  Properties properties = null;
  try {
    Resource resource = new ClassPathResource(YML_FILE_01);
    YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
    yamlFactory.setResources(resource);
    properties = yamlFactory.getObject();
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
  return properties.get(key);
}

2.加载多个配置文件

public static Properties loadProperties(String... ymlFliePaths) {
  Resource[] resources = null;
  Properties properties = null;
  List<Resource> resourcesList = new ArrayList<Resource>();
  for (String location : ymlFliePaths) {
    Resource resource = new ClassPathResource(location);
    resourcesList.add(resource);
  }
  resources = resourcesList.toArray(new Resource[resourcesList.size()]);
  try {
    YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
    yamlFactory.setResources(resources);
    properties = yamlFactory.getObject();
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
  return properties;
}

3.测试main函数

public static void main(String [] args){
    System.out.println("测试开始......");
    System.out.println("单个文件,加载application.yml,打印server.servlet.context-path值:");
    System.out.println(getYmlPropertyValue("server.servlet.context-path").toString());
    System.out.println("加载多个文件:");
    Properties properties = loadProperties(YML_FILE_01,YML_FILE_02);
    System.out.println("打印application.yml中的server.servlet.context-path值:");
    System.out.println(properties.get("server.servlet.context-path").toString());
    System.out.println("打印application-common.yml中的plat.node.service值:");
    System.out.println(properties.get("plat.node.service").toString());
    System.out.println("测试结束......");
}

测试结果:

4.完整测试用例

public class YmlPropertiesLoader {

    private static String YML_FILE_01 = "application.yml";
    private static String YML_FILE_02 = "application-common.yml";
    /**
     * 根据key获取value值
     * 加载一个配置文件
     */
    public static Object getYmlPropertyValue(String key) {

        Properties properties = null;
        try {
            Resource resource = new ClassPathResource(YML_FILE_01);
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties = yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return properties.get(key);
    }
    /**
     * 根据key获取value值
     * 加载多个配置文件
     */
    public static Properties loadProperties(String... ymlFliePaths) {
        Resource[] resources = null;
        Properties properties = null;
        List<Resource> resourcesList = new ArrayList<Resource>();
        for (String location : ymlFliePaths) {
            Resource resource = new ClassPathResource(location);
            resourcesList.add(resource);
        }
        resources = resourcesList.toArray(new Resource[resourcesList.size()]);
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resources);
            properties = yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return properties;
    }
    //测试主函数
    public static void main(String [] args){
        System.out.println("测试开始......");
        System.out.println("单个文件,加载application.yml,打印server.servlet.context-path值:");
        System.out.println(getYmlPropertyValue("server.servlet.context-path").toString());
        System.out.println("加载多个文件:");
        Properties properties = loadProperties(YML_FILE_01,YML_FILE_02);
        System.out.println("打印application.yml中的server.servlet.context-path值:");
        System.out.println(properties.get("server.servlet.context-path").toString());
        System.out.println("打印application-common.yml中的plat.node.service值:");
        System.out.println(properties.get("plat.node.service").toString());
        System.out.println("测试结束......");
    }
}

以上,感谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值