Skip to content

Commit 818451b

Browse files
quaffspencergibb
andauthored
Make InstanceRegisteredEvent implements ResolvableTypeProvider (#1468)
Fixes GH-1322 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com> Signed-off-by: Spencer Gibb <sgibb@pivotal.io> Co-authored-by: Spencer Gibb <sgibb@pivotal.io>
1 parent 3935f78 commit 818451b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstanceRegisteredEvent.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
package org.springframework.cloud.client.discovery.event;
1818

1919
import org.springframework.context.ApplicationEvent;
20+
import org.springframework.core.ResolvableType;
21+
import org.springframework.core.ResolvableTypeProvider;
2022

2123
/**
2224
* Event to be published after the local service instance registers itself with a
2325
* discovery service.
2426
*
2527
* @param <T> - type of configuration
2628
* @author Spencer Gibb
29+
* @author Yanming Zhou
2730
*/
2831
@SuppressWarnings("serial")
29-
public class InstanceRegisteredEvent<T> extends ApplicationEvent {
32+
public class InstanceRegisteredEvent<T> extends ApplicationEvent implements ResolvableTypeProvider {
3033

3134
private T config;
3235

@@ -44,4 +47,8 @@ public T getConfig() {
4447
return this.config;
4548
}
4649

50+
@Override
51+
public ResolvableType getResolvableType() {
52+
return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(this.config));
53+
}
4754
}

spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
/**
4040
* @author Spencer Gibb
4141
* @author Tim Ysewyn
42+
* @author Yanming Zhou
4243
*/
4344
// @checkstyle:off
4445
@SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, properties = "management.port=0",
@@ -49,6 +50,9 @@ public class AbstractAutoServiceRegistrationTests {
4950
@Autowired
5051
public PostEventListener postEventListener;
5152

53+
@Autowired
54+
public TestRegistrationPostEventListener testRegistrationPostEventListener;
55+
5256
@Autowired
5357
private TestAutoServiceRegistration autoRegistration;
5458

@@ -81,6 +85,8 @@ public void eventsFireTest() {
8185
then(this.preEventListener.registration.getServiceId()).isEqualTo("testRegistration2");
8286
then(this.postEventListener.wasFired).isTrue();
8387
then(this.postEventListener.config.getServiceId()).isEqualTo("testRegistration2");
88+
then(this.testRegistrationPostEventListener.wasFired).isTrue();
89+
then(this.testRegistrationPostEventListener.config.getServiceId()).isEqualTo("testRegistration2");
8490
}
8591

8692
@EnableAutoConfiguration
@@ -102,6 +108,11 @@ public PostEventListener postEventListener() {
102108
return new PostEventListener();
103109
}
104110

111+
@Bean
112+
public TestRegistrationPostEventListener testRegistrationPostEventListener() {
113+
return new TestRegistrationPostEventListener();
114+
}
115+
105116
}
106117

107118
public static class PreEventListener implements ApplicationListener<InstancePreRegisteredEvent> {
@@ -132,6 +143,20 @@ public void onApplicationEvent(InstanceRegisteredEvent event) {
132143

133144
}
134145

146+
public static class TestRegistrationPostEventListener implements ApplicationListener<InstanceRegisteredEvent<TestRegistration>> {
147+
148+
public boolean wasFired = false;
149+
150+
public TestRegistration config;
151+
152+
@Override
153+
public void onApplicationEvent(InstanceRegisteredEvent<TestRegistration> event) {
154+
this.config = event.getConfig();
155+
this.wasFired = true;
156+
}
157+
158+
}
159+
135160
public static class TestRegistration implements Registration {
136161

137162
@Override

0 commit comments

Comments
 (0)