Migrate @EventListener to ApplicationListener

Closes gh-14041
This commit is contained in:
Stephane Nicoll
2018-08-10 12:52:14 +02:00
parent 9d40df9a2a
commit 0d04d7adf8
3 changed files with 80 additions and 22 deletions

View File

@@ -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<ClassPathChangedEvent> {
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;
}
}

View File

@@ -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<ClassPathChangedEvent> {
@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));