Understanding an Axon library's Java Idiom involving Interface method and Lambda expression for configuration of TokenStore

Viewed 14

I have a question on Java Syntax on lambda expressions and the java.util.function.Function Object. Also probably Spring Dependency Injection. It is an idiom that I don't know and I hope somebody can explain it to me. I know (probably only the basics) about that idiom of generic@FunctionalInterface(s) and "implementations" through lambda expressions. This use-case seems similar, but still different.

In the example, the Function<T,R>-Object is the Type of a parameter of a method of a configuration class with a parameter name. The argument is the lambda expression config -> tokenStore.

But please look at the information provided below. Here are my questions.

  1. What config is passed as the argument? There is no "config" symbol there. So I guess it is injected by the Spring IoC Container.
  2. And what it obviously returns is the symbol tokenStore that is bound to an org.axonframework.eventhandling.tokenstore.TokenStore Instance created by theJpaTokenStore#build method. There is no connection to the config argument. Or is there?
  3. To sum it up: Just "Why?" Why are you passing a lambda expression where there is no obvious (to me) connection between the argument and the return value and why are you passing a Function-placeholder to an Interface-method - how do you call it... the head of the method? Ultimately: What is that doing because to someone like me who obviously doesn't know the idiom it looks like the answer is: nothing ?

So, there is this example on how to configure a TokenStore from the Axon Framework and I would like to understand how it works:

public class AxonConfig { 
    // ...
    public void registerTokenStore(EventProcessingConfigurer processingConfigurer) {
        TokenStore tokenStore = JpaTokenStore.builder()
                                             // …
                                             .build();
    
        processingConfigurer.registerTokenStore(config -> tokenStore);
    }
}

source: https://docs.axoniq.io/reference-guide/axon-framework/events/event-processors/streaming#token-store

And here is the source code for the EventProcessingConfigurer#registerTokenStore (GitHub) method.

public interface EventProcessingConfigurer {

// ...

   EventProcessingConfigurer registerTokenStore(Function<Configuration, TokenStore> tokenStore);

// ...

}

btw: can I pass in an argument for the code block to tell the forum software which language it is?

Thanks vonSpotz

0 Answers
Related