/*
* Copyright (c) Honor Device Co., Ltd. 2022-2022. All rights reserved.
*/
package com.hihonor.it.pur.supinfo.interfaces.facade.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;
@Component
public class DecodeEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String env = environment.getProperty("spring.profiles.active");
MutablePropertySources sources = environment.getPropertySources();
PropertySource<?> applicationProperty = sources.get("applicationConfig: [classpath:/application.properties]");
Object source = applicationProperty.getSource();
System.out.println(source);
Map map = new HashMap<>();
map.put("datasource.password.1", "fewgewgew");
MapPropertySource newSource = new MapPropertySource("application-decode", map);
environment.getPropertySources().addBefore(applicationProperty.getName(), newSource);
System.out.println(newSource);
System.out.println("pwd: " + environment.getProperty("datasource.password.1"));
}
}
在class目录下必须要有约定的文件配置META-INF/spring.factories
org.springframework.boot.env.EnvironmentPostProcessor=com.test.it.interfaces.facade.controller.DecodeEnvironmentPostProcessor