Search code examples
spring-kafka

SPEL expression in @EventListener


The below code sample is taken from Spring documentation. My question is how to pass a variable in place of 'qux-'. I tried #{}, ${}.

@EventListener(condition = "event.listenerId.startsWith('qux-')")
    public void eventHandler(ListenerContainerIdleEvent event) {
        ...
    }

Solution

  • It can be done, but indirectly...

    @Bean
    String prefix(@Value("${foo:qux-}") String foo) {
        return foo;
    }
    

    and

    @EventListener(condition = "event.listenerId.startsWith(@prefix)")
    

    It will use qux- if there is no property foo.