From 0d04d7adf81845d487f5b5624712b4af5b6071ae Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 10 Aug 2018 12:52:14 +0200 Subject: [PATCH] Migrate @EventListener to ApplicationListener Closes gh-14041 --- .../LocalDevToolsAutoConfiguration.java | 47 ++++++++++++++----- .../client/RemoteClientConfiguration.java | 9 ++-- ...SpringApplicationAdminMXBeanRegistrar.java | 46 +++++++++++++++--- 3 files changed, 80 insertions(+), 22 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java index 4f6f1f915c..6ae982153a 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java @@ -35,10 +35,14 @@ import org.springframework.boot.devtools.livereload.LiveReloadServer; import org.springframework.boot.devtools.restart.ConditionalOnInitializedRestarter; import org.springframework.boot.devtools.restart.RestartScope; import org.springframework.boot.devtools.restart.Restarter; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextRefreshedEvent; -import org.springframework.context.event.EventListener; +import org.springframework.context.event.GenericApplicationListener; +import org.springframework.core.ResolvableType; +import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; /** @@ -94,7 +98,8 @@ public class LocalDevToolsAutoConfiguration { */ @Configuration @ConditionalOnProperty(prefix = "spring.devtools.restart", name = "enabled", matchIfMissing = true) - static class RestartConfiguration { + static class RestartConfiguration + implements ApplicationListener { private final DevToolsProperties properties; @@ -102,8 +107,8 @@ public class LocalDevToolsAutoConfiguration { this.properties = properties; } - @EventListener - public void onClassPathChanged(ClassPathChangedEvent event) { + @Override + public void onApplicationEvent(ClassPathChangedEvent event) { if (event.isRestartRequired()) { Restarter.getInstance().restart( new FileWatchingFailureHandler(fileSystemWatcherFactory())); @@ -161,7 +166,7 @@ public class LocalDevToolsAutoConfiguration { } - static class LiveReloadServerEventListener { + static class LiveReloadServerEventListener implements GenericApplicationListener { private final OptionalLiveReloadServer liveReloadServer; @@ -169,16 +174,36 @@ public class LocalDevToolsAutoConfiguration { this.liveReloadServer = liveReloadServer; } - @EventListener - public void onContextRefreshed(ContextRefreshedEvent event) { - this.liveReloadServer.triggerReload(); + @Override + public boolean supportsEventType(ResolvableType eventType) { + Class type = eventType.getRawClass(); + if (type == null) { + return false; + } + return ContextRefreshedEvent.class.isAssignableFrom(type) + || ClassPathChangedEvent.class.isAssignableFrom(type); } - @EventListener - public void onClassPathChanged(ClassPathChangedEvent event) { - if (!event.isRestartRequired()) { + @Override + public boolean supportsSourceType(@Nullable Class sourceType) { + return true; + } + + @Override + public void onApplicationEvent(ApplicationEvent event) { + if (event instanceof ContextRefreshedEvent) { this.liveReloadServer.triggerReload(); } + if (event instanceof ClassPathChangedEvent) { + if (!((ClassPathChangedEvent) event).isRestartRequired()) { + this.liveReloadServer.triggerReload(); + } + } + } + + @Override + public int getOrder() { + return 0; } } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/RemoteClientConfiguration.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/RemoteClientConfiguration.java index ada9daf3ca..4e1acd6a5d 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/RemoteClientConfiguration.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/RemoteClientConfiguration.java @@ -49,9 +49,9 @@ import org.springframework.boot.devtools.livereload.LiveReloadServer; import org.springframework.boot.devtools.restart.DefaultRestartInitializer; import org.springframework.boot.devtools.restart.RestartScope; import org.springframework.boot.devtools.restart.Restarter; +import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.event.EventListener; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; @@ -131,7 +131,8 @@ public class RemoteClientConfiguration implements InitializingBean { */ @Configuration @ConditionalOnProperty(prefix = "spring.devtools.livereload", name = "enabled", matchIfMissing = true) - static class LiveReloadConfiguration { + static class LiveReloadConfiguration + implements ApplicationListener { @Autowired private DevToolsProperties properties; @@ -155,8 +156,8 @@ public class RemoteClientConfiguration implements InitializingBean { Restarter.getInstance().getThreadFactory()); } - @EventListener - public void onClassPathChanged(ClassPathChangedEvent event) { + @Override + public void onApplicationEvent(ClassPathChangedEvent event) { String url = this.remoteUrl + this.properties.getRemote().getContextPath(); this.executor.execute(new DelayedLiveReloadTrigger(optionalLiveReloadServer(), this.clientHttpRequestFactory, url)); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java index 98c04a0d70..732b454ba4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,11 +32,15 @@ import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationEvent; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.EnvironmentAware; -import org.springframework.context.event.EventListener; +import org.springframework.context.event.GenericApplicationListener; +import org.springframework.core.Ordered; +import org.springframework.core.ResolvableType; import org.springframework.core.env.Environment; import org.springframework.core.env.StandardEnvironment; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** @@ -48,7 +52,7 @@ import org.springframework.util.Assert; * @since 1.3.0 */ public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContextAware, - EnvironmentAware, InitializingBean, DisposableBean { + GenericApplicationListener, EnvironmentAware, InitializingBean, DisposableBean { private static final Log logger = LogFactory.getLog(SpringApplicationAdmin.class); @@ -80,15 +84,43 @@ public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContext this.environment = environment; } - @EventListener - public void onApplicationReadyEvent(ApplicationReadyEvent event) { + @Override + public boolean supportsEventType(ResolvableType eventType) { + Class type = eventType.getRawClass(); + if (type == null) { + return false; + } + return ApplicationReadyEvent.class.isAssignableFrom(type) + || WebServerInitializedEvent.class.isAssignableFrom(type); + } + + @Override + public boolean supportsSourceType(@Nullable Class sourceType) { + return true; + } + + @Override + public void onApplicationEvent(ApplicationEvent event) { + if (event instanceof ApplicationReadyEvent) { + onApplicationReadyEvent((ApplicationReadyEvent) event); + } + if (event instanceof WebServerInitializedEvent) { + onWebServerInitializedEvent((WebServerInitializedEvent) event); + } + } + + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE; + } + + void onApplicationReadyEvent(ApplicationReadyEvent event) { if (this.applicationContext.equals(event.getApplicationContext())) { this.ready = true; } } - @EventListener - public void onWebServerInitializedEvent(WebServerInitializedEvent event) { + void onWebServerInitializedEvent(WebServerInitializedEvent event) { if (this.applicationContext.equals(event.getApplicationContext())) { this.embeddedWebApplication = true; }