Search code examples
spring-data-jpaspring-elspring-data-redis

How to invoke non-static method to get redis key using spel?


I was reading spring documentation to create custom keys for redis cache.

https://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/cache.html

One of the example was

@Cacheable(value="books", key="T(someType).hash(#isbn)")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

Now this is valid if i want to generate key using hash method of static class. But how to generate key using method of non static class ?

class A{

@Cacheable(value="books", key="(A).hash(#isbn)")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)


String hash(){

return ..some logic;

}

I tried this way..it was not working..


Solution

  • You need to use the bean name to reference an instance.

    @Cacheable(value="books", key="@aBeanName.hash(#isbn)")