mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Migrate @EventListener to ApplicationListener
Closes gh-14041
This commit is contained in:
parent
9d40df9a2a
commit
0d04d7adf8
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user