获取SpringBoot不同配置文件的值

153 阅读1分钟

在springboot中,我们都会将配置信息放到配置文件中,这个时候就涉及到如何获取里面的值等一列问题,当然spring社区也提供了获取的方式,但是不足以满足使用者的需求,所以采用如下的工具类,可以获取不同配置文件类型的数据。

package com.angel.item.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;

public class PropertiesUtils {
    /**
     * 默认yml后缀文件名称
     */
    private final static String DEFAULT_YML_NAME = "application.yml";
    /**
     * 默认properties后缀文件名称
     */
    private final static String DEFAULT_PROPERTIES_NAME = "application.properties";

    /**
     * 获取yml配置文件中的值
     *
     * @param key 需要获取的键名
     * @return 对应的值
     */
    public static String getConfigByYml(String key) {
        Resource resource = new ClassPathResource(DEFAULT_YML_NAME);
        Properties properties = null;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties = yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
        }
        return properties.getProperty(key);
    }

    /**
     * 获取yml配置文件中的值
     *
     * @param key        需要获取的键名
     * @param configName 自定义配置文件yml后缀的名称
     * @return 对应的值
     */
    public static String getConfigByYml(String key, String configName) {
        if (StringUtils.isBlank(configName)) {
            throw new RuntimeException(configName + "不能为空");
        }
        Resource resource = new ClassPathResource(configName);
        Properties properties = null;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties = yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
        }
        return properties.getProperty(key);
    }

    /**
     * 获取properties配置文件中的值
     *
     * @param key 需要获取的键
     * @return 对应的值
     */
    public static String getConfigByProperties(String key) {
        Properties properties = null;
        try {
            properties = PropertiesLoaderUtils.loadAllProperties(DEFAULT_PROPERTIES_NAME);
        } catch (IOException e) {
            e.printStackTrace();
            e.getMessage();
        }
        return properties.getProperty(key);
    }

    /**
     * 获取properties配置文件中的值
     *
     * @param key        需要获取的键
     * @param configName properties配置文件的名称
     * @return 对应的值
     */
    public static String getConfigByProperties(String key, String configName) {
        if (StringUtils.isBlank(configName)) {
            throw new RuntimeException(configName + "不能为空");
        }
        Properties properties = null;
        try {
            properties = PropertiesLoaderUtils.loadAllProperties(configName);
        } catch (IOException e) {
            e.printStackTrace();
            e.getMessage();
        }
        return properties.getProperty(key);
    }
}