public class AfterConfigListener implements SmartApplicationListener,Ordered {
// 复制的ConfigFileApplicationListener文件内容
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType) || ApplicationPreparedEvent.class.isAssignableFrom(eventType);
}
public void onApplicationEvent(ApplicationEvent event) {
// ApplicationEnvironmentPreparedEvent 是加载配置文件,初始化日志系统的事件。
if (event instanceof ApplicationEnvironmentPreparedEvent) {
// 获得原来的password内容
String password = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment().getProperty("spring.datasource.password");
// 进行密码的解密
System.setProperty("spring.datasource.password", SM4Utils.encryptData_ECB(password));
}
if (event instanceof ApplicationPreparedEvent) {
}
}
@Override
public int getOrder() {
// 设置该监听器 在加载配置文件之后执行
return ConfigFileApplicationListener.DEFAULT_ORDER 1;
}
}
并且在main方法中加入该监听器
代码语言:javascript复制
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.addListeners(new AfterConfigListener());
springApplication.run(args);
}
}