spring-cloud / spring-cloud-commons

Common classes used in different Spring Cloud implementations
Apache License 2.0
701 stars 697 forks source link

InstanceRegisteredEvent can implement ResolvableTypeProvider interface for better convenience #1322

Open XhstormR opened 7 months ago

XhstormR commented 7 months ago

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