Skip to content

Commit 1d64629

Browse files
committed
Moves away from @eventlistener to interface implementations for framework 5.1 changes
fixes spring-projectsgh-405
1 parent 23d7eae commit 1d64629

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package org.springframework.cloud.client.serviceregistry;
22

3+
import java.util.concurrent.atomic.AtomicBoolean;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
import javax.annotation.PreDestroy;
7+
38
import org.apache.commons.logging.Log;
49
import org.apache.commons.logging.LogFactory;
10+
511
import org.springframework.beans.BeansException;
612
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
713
import org.springframework.boot.web.context.WebServerInitializedEvent;
814
import org.springframework.cloud.client.discovery.ManagementServerPortUtils;
915
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
1016
import org.springframework.context.ApplicationContext;
1117
import org.springframework.context.ApplicationContextAware;
12-
import org.springframework.context.event.EventListener;
18+
import org.springframework.context.ApplicationListener;
1319
import org.springframework.core.env.Environment;
1420

15-
import javax.annotation.PreDestroy;
16-
import java.util.concurrent.atomic.AtomicBoolean;
17-
import java.util.concurrent.atomic.AtomicInteger;
18-
1921
/**
2022
* Lifecycle methods that may be useful and common to {@link ServiceRegistry}
2123
* implementations.
@@ -27,7 +29,7 @@
2729
* @author Spencer Gibb
2830
*/
2931
public abstract class AbstractAutoServiceRegistration<R extends Registration>
30-
implements AutoServiceRegistration, ApplicationContextAware {
32+
implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener<WebServerInitializedEvent> {
3133
private static final Log logger = LogFactory
3234
.getLog(AbstractAutoServiceRegistration.class);
3335

@@ -60,7 +62,13 @@ protected ApplicationContext getContext() {
6062
return context;
6163
}
6264

63-
@EventListener(WebServerInitializedEvent.class)
65+
@Override
66+
@SuppressWarnings("deprecation")
67+
public void onApplicationEvent(WebServerInitializedEvent event) {
68+
bind(event);
69+
}
70+
71+
@Deprecated
6472
public void bind(WebServerInitializedEvent event) {
6573
ApplicationContext context = event.getApplicationContext();
6674
if (context instanceof ConfigurableWebServerApplicationContext) {

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package org.springframework.cloud.endpoint;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertTrue;
22-
2319
import java.lang.reflect.Field;
2420
import java.util.ArrayList;
2521
import java.util.Collection;
@@ -28,8 +24,8 @@
2824
import java.util.Map;
2925

3026
import org.junit.After;
31-
import org.junit.Ignore;
3227
import org.junit.Test;
28+
3329
import org.springframework.boot.Banner.Mode;
3430
import org.springframework.boot.WebApplicationType;
3531
import org.springframework.boot.builder.SpringApplicationBuilder;
@@ -43,14 +39,18 @@
4339
import org.springframework.context.ApplicationEvent;
4440
import org.springframework.context.ConfigurableApplicationContext;
4541
import org.springframework.context.annotation.Configuration;
46-
import org.springframework.context.event.EventListener;
42+
import org.springframework.context.event.SmartApplicationListener;
4743
import org.springframework.core.env.Environment;
4844
import org.springframework.core.env.MapPropertySource;
4945
import org.springframework.core.env.PropertySource;
5046
import org.springframework.stereotype.Component;
5147
import org.springframework.util.ClassUtils;
5248
import org.springframework.util.ReflectionUtils;
5349

50+
import static org.junit.Assert.assertEquals;
51+
import static org.junit.Assert.assertFalse;
52+
import static org.junit.Assert.assertTrue;
53+
5454
/**
5555
* @author Dave Syer
5656
* @author Venil Noronha
@@ -132,7 +132,6 @@ public void springMainSourcesEmptyInRefreshCycle() throws Exception {
132132
}
133133

134134
@Test
135-
@Ignore //FIXME: 2.1.0
136135
public void eventsPublishedInOrder() throws Exception {
137136
this.context = new SpringApplicationBuilder(Empty.class)
138137
.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run();
@@ -148,7 +147,6 @@ public void eventsPublishedInOrder() throws Exception {
148147
}
149148

150149
@Test
151-
@Ignore //FIXME: 2.1.0
152150
public void shutdownHooksCleaned() {
153151
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(Empty.class)
154152
.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) {
@@ -174,17 +172,22 @@ private int countShutdownHooks() {
174172
}
175173

176174
@Configuration
177-
protected static class Empty {
175+
protected static class Empty implements SmartApplicationListener {
178176
private List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
179177

180-
@EventListener(EnvironmentChangeEvent.class)
181-
public void changed(EnvironmentChangeEvent event) {
182-
this.events.add(event);
178+
179+
@Override
180+
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
181+
return EnvironmentChangeEvent.class.isAssignableFrom(eventType)
182+
|| RefreshScopeRefreshedEvent.class.isAssignableFrom(eventType);
183183
}
184184

185-
@EventListener(RefreshScopeRefreshedEvent.class)
186-
public void refreshed(RefreshScopeRefreshedEvent event) {
187-
this.events.add(event);
185+
@Override
186+
public void onApplicationEvent(ApplicationEvent event) {
187+
if (event instanceof EnvironmentChangeEvent ||
188+
event instanceof RefreshScopeRefreshedEvent) {
189+
this.events.add(event);
190+
}
188191
}
189192
}
190193

0 commit comments

Comments
 (0)