Aggregate cache auto-configuration

Hello, I have a basic question about the configuration of a cache for an aggregate repository.

When should a define the beans for the repository and what is required in the @Aggregate annotation?

What is the difference, if any, between the configurations below?

@Configuration
public class MyConfiguration {
    @Bean
    public Cache aggregateCache() {
        return new WeakReferenceCache();
    }

    @Bean
    public Repository<RequestEntity> myAggregateRepository(EventStore eventStore, Cache cache) {
        return CachingEventSourcingRepository.builder(MyAggregate.class).cache(cache).eventStore(eventStore).build();
    }
}

@Aggregate(cache = "aggregateCache", repository = "myAggregateRepository")
public class MyAggregate {
// state, CommandHandler, EventSourcingHandler
}
@Configuration
public class MyConfiguration {
    @Bean
    public Cache aggregateCache() {
        return new WeakReferenceCache();
    }
}

@Aggregate(cache = "aggregateCache")
public class MyAggregate {
// state, CommandHandler, EventSourcingHandler
}
@Configuration
public class MyConfiguration {
    @Bean
    public Cache aggregateCache() {
        return new WeakReferenceCache();
    }

    @Bean
    public Repository<RequestEntity> myAggregateRepository(EventStore eventStore, Cache cache) {
        return EventSourcingRepository.builder(MyAggregate.class).cache(cache).eventStore(eventStore).build();
    }
}

@Aggregate
public class MyAggregate {
// state, CommandHandler, EventSourcingHandler
}

I’m not sure if I fully understand the documentation here. It seems to me that they would all result in the same setup with a CachingEventSourcingRepository for MyAggregate with a cache associated.

There is no difference between the three configurations above. Defining the cache property on the @Aggregate annotation only makes sense when you leave the creation of the actual repository to Axon itself. As the cache is behavior defined on the repository, it is ignored when the repository attribute is provided.

Therefore, the first configuration is not recommended, as it provides configuration elements that are ignored, and may lead to confusion. Removing the cache attribute from the annotation will improve it. The result would be a configuration where all elements are very explicitly created (instead of using conventions and default). Some people prefer explicit configuration over conventions.

The last configuration uses convention over configuration, where a repository is defined using the naming convention for aggregates.

The middle configuration is my personal favorite. It leaves the creation of the repository to Axon but does define a clear expectation of using a cache for it.