spring-cloud-commons icon indicating copy to clipboard operation
spring-cloud-commons copied to clipboard

InstanceRegisteredEvent can implement ResolvableTypeProvider interface for better convenience

Open XhstormR opened this issue 2 years ago • 1 comments

Due to Java type erasure, currently I have to manually determine the type of the event's config when listening to this event:

@Component
class EurekaInstanceConfigPostProcessor(
    private val eureka: EurekaRegistration,
) : ApplicationListener<InstanceRegisteredEvent<*>> {

    override fun onApplicationEvent(event: InstanceRegisteredEvent<*>) {
        if (event.config is EurekaInstanceConfigBean){
            eureka.applicationInfoManager.registerAppMetadata(......)
        }
    }
}

If InstanceRegisteredEvent can implement ResolvableTypeProvider, i can directly listening and set it's config type:

@Component
class EurekaInstanceConfigPostProcessor(
    private val eureka: EurekaRegistration,
) : ApplicationListener<InstanceRegisteredEvent<EurekaInstanceConfigBean>> {

    override fun onApplicationEvent(event: InstanceRegisteredEvent<EurekaInstanceConfigBean>) {
        eureka.applicationInfoManager.registerAppMetadata(......)
    }
}

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/ResolvableTypeProvider.html

XhstormR avatar Jan 17 '24 10:01 XhstormR

It should be fixed by https://github.com/spring-cloud/spring-cloud-commons/pull/1468.

quaff avatar Feb 07 '25 09:02 quaff