在使用spring boot做开发时,有时我们需要自定义环境变量或者编写第三方扩展点,可以使用EnvironmentPostProcessor,注意如果你只是基本的使用环境,就不需要看此文了 。
1. 属性文件myapp.properties,可以自定义文件名,也可以有多个同名文件
config/myapp.properties文件
app.url=https://github.com/dongguangming/
app.name=dgm
app.desc=dongguangming github
app.customComponentScanPackages=com.spring.mapper,com.spring.mapper
mysqluser=rootroot
mysqlpwd=cstorfscstorfs
app.who=who are you
myapp.properties.文件
app.url=https://github.com/dongguangming//
app.name=dgmdgm
app.desc=dongguangming github
app.customComponentScanPackages=com.spring.mapper,com.spring.mapper
2. 新建java实现类
/**
* @author dgm
* @describe "环境变量扩展点"
* @date 2020年10月12日
*/
@Order(Ordered.LOWEST_PRECEDENCE)
public class MyJsonEnvironmentPostProcessor implements EnvironmentPostProcessor {
private static final Logger logger = LoggerFactory
.getLogger(MyJsonEnvironmentPostProcessor.class);
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
private static final String DEFAULT_NAMES = "myapp";
private static final String DEFAULT_FILE_EXTENSION = ".properties";
private static final String PREFIX = "com.spring.environmentpostprocessor.";
private static final String CALCUATION_MODE = "calculation_mode";
private static final String GROSS_CALCULATION_TAX_RATE = "gross_calculation_tax_rate";
private static final String CALCUATION_MODE_DEFAULT_VALUE = "NET";
private static final double GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE = 0;
List<String> names = Arrays.asList(CALCUATION_MODE,
GROSS_CALCULATION_TAX_RATE);
private static Map<String, Object> defaults = new LinkedHashMap<>();
static {
defaults.put(CALCUATION_MODE, CALCUATION_MODE_DEFAULT_VALUE);
defaults.put(GROSS_CALCULATION_TAX_RATE,
GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE);
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
/*
* PropertySource<?> system = environment.getPropertySources()
* .get(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
*
* Map<String, Object> prefixed = new LinkedHashMap<>();
*
* if (!hasOurPriceProperties(system)) { // Baeldung-internal code so
* this doesn't break other examples logger.error(
* "System environment variables [calculation_mode,gross_calculation_tax_rate] not detected, fallback to default value [calcuation_mode={},gross_calcuation_tax_rate={}]"
* , CALCUATION_MODE_DEFAULT_VALUE,
* GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE); prefixed = names.stream()
* .collect(Collectors.toMap(this::rename, this::getDefaultValue));
*
* environment.getPropertySources()
* .addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new
* MapPropertySource("prefixer", prefixed));
*
* return; }
*
* prefixed = names.stream() .collect(Collectors.toMap(this::rename,
* system::getProperty)); environment.getPropertySources()
* .addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new
* MapPropertySource("prefixer", prefixed));
*/
List<String> list = Arrays
.asList(StringUtils.trimArrayElements(StringUtils
.commaDelimitedListToStringArray(DEFAULT_SEARCH_LOCATIONS)));
Collections.reverse(list);
Set<String> reversedLocationSet = new LinkedHashSet(list);
System.err.println(reversedLocationSet);
ResourceLoader defaultResourceLoader = new DefaultResourceLoader();
// YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new
// YamlPropertiesFactoryBean();
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
List<Properties> loadedProperties = new ArrayList<>(16);
// List<Resource> propertiesPathList = new ArrayList<>(16);
reversedLocationSet.forEach(location -> {
Resource resource = defaultResourceLoader.getResource(location
+ DEFAULT_NAMES + DEFAULT_FILE_EXTENSION);
System.err.println(location + DEFAULT_NAMES
+ DEFAULT_FILE_EXTENSION);
if (resource == null || !resource.exists()) {
return;
}
System.err.println("################33");
Properties p = new Properties();
try {
InputStream inputStream = resource.getInputStream();
p.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
loadedProperties.add(p);
});
System.err.println(loadedProperties);
Properties filteredProperties = new Properties();
Set<Objec