# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
/**
* 指定支持哪些类型的事件
*/
boolean supportsEventType(Class<? extends ApplicationEvent> var1);
/**
* 指定支持发生事件所在的类型
*/
boolean supportsSourceType(Class<?> var1);
public class AfterConfigListener implements SmartApplicationListener,Ordered {
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType) || ApplicationPreparedEvent.class.isAssignableFrom(eventType);
}
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
}
if (event instanceof ApplicationPreparedEvent) {
}
}
@Override
public int getOrder() {
// 写在加载配置文件之后
return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
}
}
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;
}
}
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.addListeners(new AfterConfigListener());
springApplication.run(args);
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有