需求:环境变量中存在密文,如mysql 密码 ,在启动时 要转成明文。
比较流行的是jasypt 加密方式。
但是这里我们自己写一个
伪代码如下:
public class CustomEnvirnmentListener implements ApplicationListener<ApplicationEnvironmentPrepareEvent>{
@Override
public void onApplicationEvent(ApplicationEnvironment event){
ConfigurableEnvironment env = event.getEnvironment();
MutablePropertySources propertySources = env.getPropertySources();
//获取配置属性值
String value = env.getProperty("port");
//覆盖属性
propertySources.addFirst(
new MapPropertySource("key", new HashMap(){{put("port",8080);}})
)
}
}