springboot擴充套件配置檔案自動載入

浪天涯&*發表於2024-11-18
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.setting.yaml.YamlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.InputStream;
import java.util.*;

@Slf4j
public class ExpandEnvironmentPostProcessor implements EnvironmentPostProcessor {
    private static final List<String> FILE_TYPE = ListUtil.of("yml", "yaml", "properties");
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        ClassLoader classLoader = ConfigurableEnvironment.class.getClassLoader();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(classLoader);
        try {
            List<Resource> resourceList = new ArrayList<>();
            for (String fileType : FILE_TYPE) {
                Resource[] resources = resolver.getResources("classpath*:*." + fileType);
                for (Resource resource : resources) {
                    String fileName = resource.getFilename();
                    String prefix = FileUtil.getPrefix(fileName);
                    if (!CharSequenceUtil.startWith(prefix, "application") && !CharSequenceUtil.startWith(prefix, "bootstrap")) {
                        resourceList.add(resource);
                    }
                }
            }
            for (Resource resource : resourceList) {
                String fileName = Optional.ofNullable(resource.getFilename()).orElse("unknown");
                String fileType = FileUtil.extName(fileName);
                InputStream inputStream = resource.getInputStream();
                Properties properties = new Properties();
                if ("properties".equalsIgnoreCase(fileType)) {
                    properties.load(inputStream);
                } else {
                    Map<?, ?> yamlMap = YamlUtil.load(inputStream, Map.class);
                    convertToProperties(Convert.toMap(String.class, Object.class, yamlMap), "", properties);
                }
                MutablePropertySources propertySources = environment.getPropertySources();
                propertySources.addFirst(new PropertiesPropertySource(fileName, properties));
            }
        } catch (Exception e) {
            log.error("ExpandEnvironmentPostProcessor error", e);
        }
    }

    private void convertToProperties(Map<String, Object> dict, String parentKey, Properties properties) {
        for (Map.Entry<String, Object> dictEntry : dict.entrySet()) {
            String key = CharSequenceUtil.isNotBlank(parentKey) ? parentKey + "." + dictEntry.getKey() : dictEntry.getKey();
            Object object = dictEntry.getValue();
            if (object instanceof Map) {
                convertToProperties(Convert.toMap(String.class, Object.class, object), key, properties);
            } else if (Objects.nonNull(object)) {
                properties.put(key, object);
            }
        }
    }
}

  

相關文章