import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.*;
import java.util.Iterator;
import java.util.Properties;
public class PropertiesInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public PropertiesInitializer() {
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
Iterator it = propertySources.iterator();
PropertySource source;
while (it.hasNext()) {
source = (PropertySource) it.next();
if (!(source instanceof EnumerablePropertySource)) {
continue;
}
EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource) source;
String[] propertyNames = enumerablePropertySource.getPropertyNames();
int length = propertyNames.length;
//获取到配置文件中的key 对key进行处理 也可以处理value,jsypt就是对ENC()包裹内容进行解密
for (int i = 0; i < length; i++) {
String name = propertyNames[i];
String value = String.valueOf(source.getProperty(name));
if (name.equals("jasypt.encryptor.replace")) {
Properties properties = new Properties();
properties.setProperty("jasypt.encryptor.password", value);
PropertySource<?> propertySource = new PropertiesPropertySource("encryptor" + source.getName(), properties);
propertySources.addBefore(source.getName(), propertySource);
break;
}
}
}
}
}
springboot初始化的时候对配置文件进行处理
最新推荐文章于 2023-08-15 11:46:29 发布