Commit 6f0d0191 authored by yanzg's avatar yanzg

自动变量处理

parent 915bd245
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;
/**
* 环境变量处理
*/
@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();
}
}
}
\ No newline at end of file
org.springframework.boot.env.EnvironmentPostProcessor=com.yanzuoguang.cloud.env.YzgEnvironmentPostProcessor
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment