David Kubecka
11/24/2023, 8:09 PMabstract const val
Daniel Pitts
11/24/2023, 9:24 PMDaniel Pitts
11/24/2023, 9:24 PMDavid Kubecka
11/24/2023, 9:33 PMabstract class SomeClass {
@SomeAnnotation(XXX)
fun someMethod()
}
class SomeClassImpl : SomeClass() {
// somehow fill in XXX without overriding someMethod
}
Daniel Pitts
11/24/2023, 9:49 PMDaniel Pitts
11/24/2023, 9:51 PMabstract class SomeClass {
fun someMethod() // not annotated
}
class SomeClassImpl : SomeClass {
@SomeAnnotation
override fun someMethod()
}
Daniel Pitts
11/24/2023, 9:52 PMDavid Kubecka
11/24/2023, 9:57 PMDaniel Pitts
11/25/2023, 2:20 AMDavid Kubecka
11/25/2023, 9:01 AM@KafkaListener
annotated method. These are almost the same in all the classes except for one annotation parameter, namely the Kafka topic name. If the listener config was not done via annotations but programmatically I would simply create a base class with the topic as constructor parameter. What I would like to do, is to achieve the same with annotations, i.e. passing the parameter from the concrete implementation to the base class. The complication here is that it must be a compile-time constant so that it can be used in the annotation.
(I know that Kafka listeners can be constructed programmatically but this brings a lot of other complexity (you have to register the listeners manually at a central place, for example), so I would like to stick with the annotation-based approach.)Sam
11/25/2023, 9:16 AM@KafkaListener
annotation supports SpEL expressions, and recognizes a special __listener
token that refers to the current instance. So you should be able to write:
@KafkaListener(topics = "#{__listener.topic}")
Then you just need a separate topic
property which you override in each subclass. Or provide as a constructor parameter like you suggested.David Kubecka
11/25/2023, 9:47 AMDavid Kubecka
11/25/2023, 9:49 AMabstract const val
) would make sense by itself? Perhaps I should ask in a different channel, though.Sam
11/25/2023, 9:54 AMDavid Kubecka
11/25/2023, 9:59 AMabstract properties are by definition resolved dynamicallyI imagined that the suggested
abstract const val
would require the subclasses to override that property with a constant, i.e. with the same restrictions as the current const val
. That way it would still be guaranteed that the concrete value is known at compile time.Sam
11/25/2023, 10:10 AMa
and b
, you can make a third property c
that dynamically resolves to one of a
or b
, but that means c
itself is not a constant, even though both of its possible values are constants.David Kubecka
11/25/2023, 10:14 AMIt needs there to be a single constant value which can be compiled into the class fileThis makes it clear to me. That's probably the main point of the constants after all - that they are compiled/inlined directly in the class file. Thanks.