YzgEnvironmentPostProcessor.java 1.52 KB
package com.yanzuoguang.cloud.env;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;


/**
 * 环境变量处理,通过服务处理环境变量
 * @author 颜佐光
 */
@Component
public class YzgEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String key = "yzg.config.url";
        String property = environment.getProperty(key);
        if (StringHelper.isEmpty(property)) {
            return;
        }
        File file = new File(property);
        if (!file.exists()) {
            return;
        }
        try (InputStream input = new FileInputStream(property)) {
            Properties properties = new Properties();
            properties.load(input);
            PropertiesPropertySource propertySource = new PropertiesPropertySource("ve", properties);
            environment.getPropertySources().addLast(propertySource);
            System.out.println("====加载外部配置文件" + property + "完毕====");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}