Commit 0d04d7ad authored by Stephane Nicoll's avatar Stephane Nicoll

Migrate @EventListener to ApplicationListener

Closes gh-14041
parent 9d40df9a
...@@ -35,10 +35,14 @@ import org.springframework.boot.devtools.livereload.LiveReloadServer; ...@@ -35,10 +35,14 @@ import org.springframework.boot.devtools.livereload.LiveReloadServer;
import org.springframework.boot.devtools.restart.ConditionalOnInitializedRestarter; import org.springframework.boot.devtools.restart.ConditionalOnInitializedRestarter;
import org.springframework.boot.devtools.restart.RestartScope; import org.springframework.boot.devtools.restart.RestartScope;
import org.springframework.boot.devtools.restart.Restarter; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent; 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; import org.springframework.util.StringUtils;
/** /**
...@@ -94,7 +98,8 @@ public class LocalDevToolsAutoConfiguration { ...@@ -94,7 +98,8 @@ public class LocalDevToolsAutoConfiguration {
*/ */
@Configuration @Configuration
@ConditionalOnProperty(prefix = "spring.devtools.restart", name = "enabled", matchIfMissing = true) @ConditionalOnProperty(prefix = "spring.devtools.restart", name = "enabled", matchIfMissing = true)
static class RestartConfiguration { static class RestartConfiguration
implements ApplicationListener<ClassPathChangedEvent> {
private final DevToolsProperties properties; private final DevToolsProperties properties;
...@@ -102,8 +107,8 @@ public class LocalDevToolsAutoConfiguration { ...@@ -102,8 +107,8 @@ public class LocalDevToolsAutoConfiguration {
this.properties = properties; this.properties = properties;
} }
@EventListener @Override
public void onClassPathChanged(ClassPathChangedEvent event) { public void onApplicationEvent(ClassPathChangedEvent event) {
if (event.isRestartRequired()) { if (event.isRestartRequired()) {
Restarter.getInstance().restart( Restarter.getInstance().restart(
new FileWatchingFailureHandler(fileSystemWatcherFactory())); new FileWatchingFailureHandler(fileSystemWatcherFactory()));
...@@ -161,7 +166,7 @@ public class LocalDevToolsAutoConfiguration { ...@@ -161,7 +166,7 @@ public class LocalDevToolsAutoConfiguration {
} }
static class LiveReloadServerEventListener { static class LiveReloadServerEventListener implements GenericApplicationListener {
private final OptionalLiveReloadServer liveReloadServer; private final OptionalLiveReloadServer liveReloadServer;
...@@ -169,16 +174,36 @@ public class LocalDevToolsAutoConfiguration { ...@@ -169,16 +174,36 @@ public class LocalDevToolsAutoConfiguration {
this.liveReloadServer = liveReloadServer; this.liveReloadServer = liveReloadServer;
} }
@EventListener @Override
public void onContextRefreshed(ContextRefreshedEvent event) { public boolean supportsEventType(ResolvableType eventType) {
this.liveReloadServer.triggerReload(); Class<?> type = eventType.getRawClass();
if (type == null) {
return false;
}
return ContextRefreshedEvent.class.isAssignableFrom(type)
|| ClassPathChangedEvent.class.isAssignableFrom(type);
}
@Override
public boolean supportsSourceType(@Nullable Class<?> sourceType) {
return true;
} }
@EventListener @Override
public void onClassPathChanged(ClassPathChangedEvent event) { public void onApplicationEvent(ApplicationEvent event) {
if (!event.isRestartRequired()) { if (event instanceof ContextRefreshedEvent) {
this.liveReloadServer.triggerReload(); this.liveReloadServer.triggerReload();
} }
if (event instanceof ClassPathChangedEvent) {
if (!((ClassPathChangedEvent) event).isRestartRequired()) {
this.liveReloadServer.triggerReload();
}
}
}
@Override
public int getOrder() {
return 0;
} }
} }
......
...@@ -49,9 +49,9 @@ import org.springframework.boot.devtools.livereload.LiveReloadServer; ...@@ -49,9 +49,9 @@ import org.springframework.boot.devtools.livereload.LiveReloadServer;
import org.springframework.boot.devtools.restart.DefaultRestartInitializer; import org.springframework.boot.devtools.restart.DefaultRestartInitializer;
import org.springframework.boot.devtools.restart.RestartScope; import org.springframework.boot.devtools.restart.RestartScope;
import org.springframework.boot.devtools.restart.Restarter; import org.springframework.boot.devtools.restart.Restarter;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor;
...@@ -131,7 +131,8 @@ public class RemoteClientConfiguration implements InitializingBean { ...@@ -131,7 +131,8 @@ public class RemoteClientConfiguration implements InitializingBean {
*/ */
@Configuration @Configuration
@ConditionalOnProperty(prefix = "spring.devtools.livereload", name = "enabled", matchIfMissing = true) @ConditionalOnProperty(prefix = "spring.devtools.livereload", name = "enabled", matchIfMissing = true)
static class LiveReloadConfiguration { static class LiveReloadConfiguration
implements ApplicationListener<ClassPathChangedEvent> {
@Autowired @Autowired
private DevToolsProperties properties; private DevToolsProperties properties;
...@@ -155,8 +156,8 @@ public class RemoteClientConfiguration implements InitializingBean { ...@@ -155,8 +156,8 @@ public class RemoteClientConfiguration implements InitializingBean {
Restarter.getInstance().getThreadFactory()); Restarter.getInstance().getThreadFactory());
} }
@EventListener @Override
public void onClassPathChanged(ClassPathChangedEvent event) { public void onApplicationEvent(ClassPathChangedEvent event) {
String url = this.remoteUrl + this.properties.getRemote().getContextPath(); String url = this.remoteUrl + this.properties.getRemote().getContextPath();
this.executor.execute(new DelayedLiveReloadTrigger(optionalLiveReloadServer(), this.executor.execute(new DelayedLiveReloadTrigger(optionalLiveReloadServer(),
this.clientHttpRequestFactory, url)); this.clientHttpRequestFactory, url));
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; ...@@ -32,11 +32,15 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EnvironmentAware; 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.Environment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
...@@ -48,7 +52,7 @@ import org.springframework.util.Assert; ...@@ -48,7 +52,7 @@ import org.springframework.util.Assert;
* @since 1.3.0 * @since 1.3.0
*/ */
public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContextAware, public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContextAware,
EnvironmentAware, InitializingBean, DisposableBean { GenericApplicationListener, EnvironmentAware, InitializingBean, DisposableBean {
private static final Log logger = LogFactory.getLog(SpringApplicationAdmin.class); private static final Log logger = LogFactory.getLog(SpringApplicationAdmin.class);
...@@ -80,15 +84,43 @@ public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContext ...@@ -80,15 +84,43 @@ public class SpringApplicationAdminMXBeanRegistrar implements ApplicationContext
this.environment = environment; this.environment = environment;
} }
@EventListener @Override
public void onApplicationReadyEvent(ApplicationReadyEvent event) { 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())) { if (this.applicationContext.equals(event.getApplicationContext())) {
this.ready = true; this.ready = true;
} }
} }
@EventListener void onWebServerInitializedEvent(WebServerInitializedEvent event) {
public void onWebServerInitializedEvent(WebServerInitializedEvent event) {
if (this.applicationContext.equals(event.getApplicationContext())) { if (this.applicationContext.equals(event.getApplicationContext())) {
this.embeddedWebApplication = true; this.embeddedWebApplication = true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment