Browse Source

Moves away from @EventListener to interface implementations for framework 5.1 changes

fixes gh-405
pull/413/head
Spencer Gibb 6 years ago
parent
commit
1d646290ab
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 22
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java
  2. 31
      spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java

22
spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java

@ -1,7 +1,13 @@
package org.springframework.cloud.client.serviceregistry; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent; 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.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.event.EventListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment; 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} * Lifecycle methods that may be useful and common to {@link ServiceRegistry}
* implementations. * implementations.
@ -27,7 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* @author Spencer Gibb * @author Spencer Gibb
*/ */
public abstract class AbstractAutoServiceRegistration<R extends Registration> public abstract class AbstractAutoServiceRegistration<R extends Registration>
implements AutoServiceRegistration, ApplicationContextAware { implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener<WebServerInitializedEvent> {
private static final Log logger = LogFactory private static final Log logger = LogFactory
.getLog(AbstractAutoServiceRegistration.class); .getLog(AbstractAutoServiceRegistration.class);
@ -60,7 +62,13 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
return context; return context;
} }
@EventListener(WebServerInitializedEvent.class) @Override
@SuppressWarnings("deprecation")
public void onApplicationEvent(WebServerInitializedEvent event) {
bind(event);
}
@Deprecated
public void bind(WebServerInitializedEvent event) { public void bind(WebServerInitializedEvent event) {
ApplicationContext context = event.getApplicationContext(); ApplicationContext context = event.getApplicationContext();
if (context instanceof ConfigurableWebServerApplicationContext) { if (context instanceof ConfigurableWebServerApplicationContext) {

31
spring-cloud-context/src/test/java/org/springframework/cloud/endpoint/RefreshEndpointTests.java

@ -16,10 +16,6 @@
package org.springframework.cloud.endpoint; 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.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -28,8 +24,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.After; import org.junit.After;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.Banner.Mode; import org.springframework.boot.Banner.Mode;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder; 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.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration; 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.Environment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
@ -51,6 +47,10 @@ import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils; 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 Dave Syer
* @author Venil Noronha * @author Venil Noronha
@ -132,7 +132,6 @@ public class RefreshEndpointTests {
} }
@Test @Test
@Ignore //FIXME: 2.1.0
public void eventsPublishedInOrder() throws Exception { public void eventsPublishedInOrder() throws Exception {
this.context = new SpringApplicationBuilder(Empty.class) this.context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run(); .web(WebApplicationType.NONE).bannerMode(Mode.OFF).run();
@ -148,7 +147,6 @@ public class RefreshEndpointTests {
} }
@Test @Test
@Ignore //FIXME: 2.1.0
public void shutdownHooksCleaned() { public void shutdownHooksCleaned() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(Empty.class) try (ConfigurableApplicationContext context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) { .web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) {
@ -174,19 +172,24 @@ public class RefreshEndpointTests {
} }
@Configuration @Configuration
protected static class Empty { protected static class Empty implements SmartApplicationListener {
private List<ApplicationEvent> events = new ArrayList<ApplicationEvent>(); private List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
@EventListener(EnvironmentChangeEvent.class)
public void changed(EnvironmentChangeEvent event) { @Override
this.events.add(event); public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return EnvironmentChangeEvent.class.isAssignableFrom(eventType)
|| RefreshScopeRefreshedEvent.class.isAssignableFrom(eventType);
} }
@EventListener(RefreshScopeRefreshedEvent.class) @Override
public void refreshed(RefreshScopeRefreshedEvent event) { public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof EnvironmentChangeEvent ||
event instanceof RefreshScopeRefreshedEvent) {
this.events.add(event); this.events.add(event);
} }
} }
}
@Component @Component
protected static class ExternalPropertySourceLocator protected static class ExternalPropertySourceLocator

Loading…
Cancel
Save