From 1d646290abb5a123e452225178a355593f85565a Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 21 Aug 2018 11:42:27 -0400 Subject: [PATCH] Moves away from @EventListener to interface implementations for framework 5.1 changes fixes gh-405 --- .../AbstractAutoServiceRegistration.java | 22 +++++++++---- .../cloud/endpoint/RefreshEndpointTests.java | 33 ++++++++++--------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java index 8fe416e9..70f68b2a 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java @@ -1,7 +1,13 @@ package org.springframework.cloud.client.serviceregistry; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import javax.annotation.PreDestroy; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.beans.BeansException; import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; import org.springframework.boot.web.context.WebServerInitializedEvent; @@ -9,13 +15,9 @@ import org.springframework.cloud.client.discovery.ManagementServerPortUtils; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; -import org.springframework.context.event.EventListener; +import org.springframework.context.ApplicationListener; import org.springframework.core.env.Environment; -import javax.annotation.PreDestroy; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; - /** * Lifecycle methods that may be useful and common to {@link ServiceRegistry} * implementations. @@ -27,7 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger; * @author Spencer Gibb */ public abstract class AbstractAutoServiceRegistration - implements AutoServiceRegistration, ApplicationContextAware { + implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener { private static final Log logger = LogFactory .getLog(AbstractAutoServiceRegistration.class); @@ -60,7 +62,13 @@ public abstract class AbstractAutoServiceRegistration return context; } - @EventListener(WebServerInitializedEvent.class) + @Override + @SuppressWarnings("deprecation") + public void onApplicationEvent(WebServerInitializedEvent event) { + bind(event); + } + + @Deprecated public void bind(WebServerInitializedEvent event) { ApplicationContext context = event.getApplicationContext(); if (context instanceof ConfigurableWebServerApplicationContext) { diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java index ca256d78..1e562431 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java @@ -16,10 +16,6 @@ package org.springframework.cloud.endpoint; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; @@ -28,8 +24,8 @@ import java.util.List; import java.util.Map; import org.junit.After; -import org.junit.Ignore; import org.junit.Test; + import org.springframework.boot.Banner.Mode; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; @@ -43,7 +39,7 @@ import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEven import org.springframework.context.ApplicationEvent; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; -import org.springframework.context.event.EventListener; +import org.springframework.context.event.SmartApplicationListener; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; @@ -51,6 +47,10 @@ import org.springframework.stereotype.Component; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + /** * @author Dave Syer * @author Venil Noronha @@ -132,7 +132,6 @@ public class RefreshEndpointTests { } @Test - @Ignore //FIXME: 2.1.0 public void eventsPublishedInOrder() throws Exception { this.context = new SpringApplicationBuilder(Empty.class) .web(WebApplicationType.NONE).bannerMode(Mode.OFF).run(); @@ -148,7 +147,6 @@ public class RefreshEndpointTests { } @Test - @Ignore //FIXME: 2.1.0 public void shutdownHooksCleaned() { try (ConfigurableApplicationContext context = new SpringApplicationBuilder(Empty.class) .web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) { @@ -174,17 +172,22 @@ public class RefreshEndpointTests { } @Configuration - protected static class Empty { + protected static class Empty implements SmartApplicationListener { private List events = new ArrayList(); - @EventListener(EnvironmentChangeEvent.class) - public void changed(EnvironmentChangeEvent event) { - this.events.add(event); + + @Override + public boolean supportsEventType(Class eventType) { + return EnvironmentChangeEvent.class.isAssignableFrom(eventType) + || RefreshScopeRefreshedEvent.class.isAssignableFrom(eventType); } - @EventListener(RefreshScopeRefreshedEvent.class) - public void refreshed(RefreshScopeRefreshedEvent event) { - this.events.add(event); + @Override + public void onApplicationEvent(ApplicationEvent event) { + if (event instanceof EnvironmentChangeEvent || + event instanceof RefreshScopeRefreshedEvent) { + this.events.add(event); + } } }