1. 程式人生 > >spring項目讀取配置的demo

spring項目讀取配置的demo

input reader system 不同 ssl init 研發 eas import

背景

讀取配置是基礎能力,研發這個模式不錯,可以從不同配置中讀取數據,如下圖:
技術分享圖片
可以根據不同分類的文件來管理配置,然後統一在conf中配置哪些文件

package com.jwen.platform.config;

import com.jwen.platform.exception.AppConfigurationException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public enum Configurations {
    INSTANCE;

    private final static String KEY_VALUE_SEPARATOR = "=";
    private final static String CONFIGURATION_FILE = "application.conf";
    private final static String OTHER_CONFIGURATION_FILE_KEY = "app_include_file";

    private List<String> otherConfigurationFiles = new ArrayList<>();
    private Map<String, String> configurations = new HashMap<>();

    Configurations() {
        try {
            init();
        } catch (IOException | URISyntaxException e) {
            throw new AppConfigurationException("has error in your application.conf", e);
        }
    }

    private void init() throws IOException, URISyntaxException {

        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(CONFIGURATION_FILE);
        BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));

        String s = "";
        List<String> lines = new ArrayList<String>();

        while ((s = br.readLine()) != null) {
            lines.add(s);
        }

        // 關閉流
        resourceAsStream.close();
        br.close();

        initConfigurations(lines);
        initOtherConfigurations();

    }

    private void initOtherConfigurations() throws URISyntaxException, IOException {
        for (String fileName : otherConfigurationFiles) {
            InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));


            String s = "";
            List<String> lines = new ArrayList<String>();

            while ((s = br.readLine()) != null) {
                lines.add(s);
            }

            // 關閉流
            resourceAsStream.close();
            br.close();


            initConfigurations(lines);
        }

        otherConfigurationFiles = null;
    }

    private boolean isOtherConfigurationKey(String key) {
        return OTHER_CONFIGURATION_FILE_KEY.equalsIgnoreCase(key);
    }

    private void initConfigurations(List<String> lines) throws URISyntaxException, IOException {
        lines.stream().forEach(line -> {
            int keyValueSeparatorIndex = line.indexOf(KEY_VALUE_SEPARATOR);
            if (isLegalConfigurationLine(line)) {
                String key = line.substring(0, keyValueSeparatorIndex).trim();
                String value = line.substring(keyValueSeparatorIndex + 1, line.length()).trim();
                if (isOtherConfigurationKey(key)) {
                    otherConfigurationFiles.add(value);
                } else {
                    configurations.put(key, value);
                }
            }
        });
    }

    private boolean isLegalConfigurationLine(String line) {
        Pattern pattern = Pattern.compile("^[a-zA-Z](.*?)=(.*?)");
        return pattern.matcher(line).matches();
    }


    public String get(String key) {
        return configurations.get(key);
    }

    public static void main(String[] args) {

        System.out.println(Configurations.INSTANCE.get("test.on"));
    }
}

spring項目讀取配置的demo