Spring Boot Application events and listeners
- In the case of multiple environments. You may need to switch a corresponding property of the configuration file to switch the environment
- The problem is how to verify the corresponding property value immediately after the spring boot loads the configuration file
SmartApplicationListener realizes monitor decoupling
- We just need to add a listener after loading. You can get the content of application.yml. Or before this incident. They can't get the corresponding content
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.
- After reading the application.yml file, an event ConfigFileApplicationListener will be triggered. The listener implements the reading of 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);
2, ConfigFileApplicationListener
3, Go straight to the subject
- 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 {
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) {
String banks = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment().getProperty("spring.name");
if (ToolUtil.isEmpty(BankEnum.getValue(banks))) {
throw new RuntimeException("Please check. com.enums.BankEnum Whether the banks Environment name!");
}
}
if (event instanceof ApplicationPreparedEvent) {
}
}
@Override
public int getOrder() {
// Write after loading configuration file
return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
}
}
- And add the listener to the main method
public class XProApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(XProApplication.class);
springApplication.addListeners(new AfterConfigListener());
springApplication.run(args);
}
}