Skip to content

Make InstanceRegisteredEvent implements ResolvableTypeProvider #1468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,16 +17,19 @@
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
* discovery service.
*
* @param <T> - type of configuration
* @author Spencer Gibb
* @author Yanming Zhou
*/
@SuppressWarnings("serial")
public class InstanceRegisteredEvent<T> extends ApplicationEvent {
public class InstanceRegisteredEvent<T> extends ApplicationEvent implements ResolvableTypeProvider {

private T config;

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

@Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(this.config));
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -39,6 +39,7 @@
/**
* @author Spencer Gibb
* @author Tim Ysewyn
* @author Yanming Zhou
*/
// @checkstyle:off
@SpringBootTest(classes = AbstractAutoServiceRegistrationTests.Config.class, properties = "management.port=0",
Expand All @@ -49,6 +50,9 @@ public class AbstractAutoServiceRegistrationTests {
@Autowired
public PostEventListener postEventListener;

@Autowired
public TestRegistrationPostEventListener testRegistrationPostEventListener;

@Autowired
private TestAutoServiceRegistration autoRegistration;

Expand Down Expand Up @@ -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
Expand All @@ -102,6 +108,11 @@ public PostEventListener postEventListener() {
return new PostEventListener();
}

@Bean
public TestRegistrationPostEventListener testRegistrationPostEventListener() {
return new TestRegistrationPostEventListener();
}

}

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

}

public static class TestRegistrationPostEventListener implements ApplicationListener<InstanceRegisteredEvent<TestRegistration>> {

public boolean wasFired = false;

public TestRegistration config;

@Override
public void onApplicationEvent(InstanceRegisteredEvent<TestRegistration> event) {
this.config = event.getConfig();
this.wasFired = true;
}

}

public static class TestRegistration implements Registration {

@Override
Expand Down
Loading