@KafkaListener Attribute Modification

Starting with version 2.7.2, you can now programmatically modify annotation attributes before the container is created. To do so, add one or more KafkaListenerAnnotationBeanPostProcessor.AnnotationEnhancer to the application context. AnnotationEnhancer is a BiFunction<Map<String, Object>, AnnotatedElement, Map<String, Object> and must return a map of attributes. The attribute values can contain SpEL and/or property placeholders; the enhancer is called before any resolution is performed. If more than one enhancer is present, and they implement Ordered, they will be invoked in order.

AnnotationEnhancer bean definitions must be declared static because they are required very early in the application context’s lifecycle.

An example follows:

@Bean
public static AnnotationEnhancer groupIdEnhancer() {
    return (attrs, element) -> {
        attrs.put("groupId", attrs.get("id") + "." + (element instanceof Class
                ? ((Class<?>) element).getSimpleName()
                : ((Method) element).getDeclaringClass().getSimpleName()
                        +  "." + ((Method) element).getName()));
        return attrs;
    };
}