From cc34965d06d779053a30e430b72d2b6bcbbb27ab Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Fri, 7 Feb 2025 17:09:14 +0800 Subject: [PATCH] Make InstanceRegisteredEvent implements ResolvableTypeProvider Fix GH-1322 Signed-off-by: Yanming Zhou --- .../event/InstanceRegisteredEvent.java | 11 ++++++-- .../AbstractAutoServiceRegistrationTests.java | 27 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstanceRegisteredEvent.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstanceRegisteredEvent.java index 1b0ee0fdd..a758e05b5 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstanceRegisteredEvent.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstanceRegisteredEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2025 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. @@ -17,6 +17,8 @@ package org.springframework.cloud.client.discovery.event; import org.springframework.context.ApplicationEvent; +import org.springframework.core.ResolvableType; +import org.springframework.core.ResolvableTypeProvider; /** * Event to be published after the local service instance registers itself with a @@ -24,9 +26,10 @@ * * @param - type of configuration * @author Spencer Gibb + * @author Yanming Zhou */ @SuppressWarnings("serial") -public class InstanceRegisteredEvent extends ApplicationEvent { +public class InstanceRegisteredEvent extends ApplicationEvent implements ResolvableTypeProvider { private T config; @@ -44,4 +47,8 @@ public T getConfig() { return this.config; } + @Override + public ResolvableType getResolvableType() { + return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(this.config)); + } } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java index ef2fa6682..f7e23a078 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2025 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. @@ -39,6 +39,7 @@ /** * @author Spencer Gibb * @author Tim Ysewyn + * @author Yanming Zhou */ // @checkstyle:off @SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, properties = "management.port=0", @@ -49,6 +50,9 @@ public class AbstractAutoServiceRegistrationTests { @Autowired public PostEventListener postEventListener; + @Autowired + public TestRegistrationPostEventListener testRegistrationPostEventListener; + @Autowired private TestAutoServiceRegistration autoRegistration; @@ -81,6 +85,8 @@ public void eventsFireTest() { then(this.preEventListener.registration.getServiceId()).isEqualTo("testRegistration2"); then(this.postEventListener.wasFired).isTrue(); then(this.postEventListener.config.getServiceId()).isEqualTo("testRegistration2"); + then(this.testRegistrationPostEventListener.wasFired).isTrue(); + then(this.testRegistrationPostEventListener.config.getServiceId()).isEqualTo("testRegistration2"); } @EnableAutoConfiguration @@ -102,6 +108,11 @@ public PostEventListener postEventListener() { return new PostEventListener(); } + @Bean + public TestRegistrationPostEventListener testRegistrationPostEventListener() { + return new TestRegistrationPostEventListener(); + } + } public static class PreEventListener implements ApplicationListener { @@ -132,6 +143,20 @@ public void onApplicationEvent(InstanceRegisteredEvent event) { } + public static class TestRegistrationPostEventListener implements ApplicationListener> { + + public boolean wasFired = false; + + public TestRegistration config; + + @Override + public void onApplicationEvent(InstanceRegisteredEvent event) { + this.config = event.getConfig(); + this.wasFired = true; + } + + } + public static class TestRegistration implements Registration { @Override