Application context生命周期事件
public class SpringEventListenerBootstrap { public static void main(String[] args) { new SpringApplicationBuilder(Object.class) .listeners( event -> System.out.println( event.getClass().getSimpleName()+ ",事件源:" + event.getSource().getClass().getName() )).web(WebApplicationType.NONE).run().close(); } }
输出:
ApplicationStartingEvent,事件源:org.springframework.boot.SpringApplication
ApplicationEnvironmentPreparedEvent,事件源:org.springframework.boot.SpringApplication
ApplicationContextInitializedEvent,事件源:org.springframework.boot.SpringApplication
ApplicationPreparedEvent,事件源:org.springframework.boot.SpringApplication
ContextRefreshedEvent,事件源:org.springframework.context.annotation.AnnotationConfig
ApplicationContextApplicationStartedEvent,事件源:org.springframework.boot.SpringApplication
ApplicationReadyEvent,事件源:org.springframework.boot.SpringApplication
ContextClosedEvent,事件源:org.springframework.context.annotation.AnnotationConfigApplicationContext
ApplicationListener
-
org.springframework.boot.ClearCachesApplicationListener
监听ContextRefreshedEvent事件,根据classLoader向上级递归查找clearCache方法,主要作用是清除ReflectionUtils和ClassLoader缓存
@Override public void onApplicationEvent(ContextRefreshedEvent event) { ReflectionUtils.clearCache(); clearClassLoaderCaches(Thread.currentThread().getContextClassLoader()); } private void clearClassLoaderCaches(ClassLoader classLoader) { if (classLoader == null