Spring Boot Application events and listeners
- Find application.yml Read operation of.
- from spring.factories View in
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
- ConfigFileApplicationListener this object pair application.yml Read operation
- Listener of ConfigFileApplicationListener event, inheriting SmartApplicationListener interface
- SmartApplicationListener interface inherits ApplicationListener and Ordered interface, which can realize orderly listening.
1, SmartApplicationListener introduction
- Spring ApplicationEvent and its corresponding Listener provide an implementation of event monitoring, publishing and subscription. The internal implementation mode is observer mode, which can decouple business between business systems, and provide system scalability, reusability and maintainability.
- stay application.yml After reading the file, an event ConfigFileApplicationListener will be triggered. This listener implements reading the file.
- SmartApplicationListener is an advanced listener, a subclass of ApplicationListener, which can realize orderly listening
- SmartApplicationListener provides two methods:
/**
* Specify which types of events are supported
*/
boolean supportsEventType(Class<? extends ApplicationEvent> var1);
/**
* Specifies the type of event that is supported
*/
boolean supportsSourceType(Class<?> var1);
How to realize monitor decoupling in SmartApplicationListener
- 1. We just need to add a listener after loading.
- 2. Inherit SmartApplicationListener interface
- 3. Setting the order property determines the order of listeners configfil eApplicationListener.DEFAULT_ ORDER + 1
- 4. Will application.yml After the content is read and modified
2, ConfigFileApplicationListener
3, Final result:
- Add a listener. Since we want to do something after the configuration file is loaded, we can directly copy the implementation of ConfigFileApplicationListener
- Delete the operation that doesn't need to be processed (the following code is approximate) and the order is after ConfigFileApplicationListener
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() {
// Write after loading configuration file
return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
}
}
- This completes the code listening after the configuration file. SmartApplicationListener implements the listening of ApplicationListener, so we can execute code in onApplicationEvent.
- The perfect code is as follows. Listen and get profile content
public class AfterConfigListener implements SmartApplicationListener,Ordered {
// Contents of the copied ConfigFileApplicationListener file
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType) || ApplicationPreparedEvent.class.isAssignableFrom(eventType);
}
public void onApplicationEvent(ApplicationEvent event) {
// ApplicationEnvironmentPreparedEvent is the event that loads the configuration file and initializes the log system.
if (event instanceof ApplicationEnvironmentPreparedEvent) {
// Get the original password content
String password = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment().getProperty("spring.datasource.password");
// Decrypt the password
System.setProperty("spring.datasource.password", SM4Utils.encryptData_ECB(password));
}
if (event instanceof ApplicationPreparedEvent) {
}
}
@Override
public int getOrder() {
// Set the listener to execute after loading the configuration file
return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
}
}
- And add the listener to the main method
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.addListeners(new AfterConfigListener());
springApplication.run(args);
}
}