Search code examples
javaspringspring-cache

set value to Cacheable annotation from property file


I'm using Spring Cacheable annotation and at the moment I'm struggling with a way of adding the cache name from property file.

I tried:

@Cacheable("${some.cache.name}")

and

@Cacheable("#{'${some.cache.name}'}")

Solution

  • There is a SPI to do that that is much more powerful than just using SpEL. You can implement CacheResolver and resolve cache instance(s) at runtime. You could use the annotated type or any name that is provided via the annotation.

    You can specify the CacheResolver per annotation, at class-level using @CacheConfig or globally by implementing CacheConfigurer.

    Check the documentation for more details