YzgEnvironmentPostProcessor.java 1.52 KB
Newer Older
yanzg's avatar
yanzg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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;


/**
yanzg's avatar
yanzg committed
17
 * 环境变量处理,通过服务处理环境变量
yanzg's avatar
yanzg committed
18
 * @author 颜佐光
yanzg's avatar
yanzg committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 */
@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();
        }

    }

}