Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring caching - How to reference a bean in SPEL to enable conditional caching via @Cacheable

I've got a method that I want to conditionally cache based on the result of a method call to another bean (which says whether or not global caching is turned on).

I've tried,using SpEL, something along the lines of

@Cacheable(condition="@someBean.isSomeBoolean()")

which requires a BeanResolver which I don't have configured. I'm OK about creating one of these programmatically but how do I configure the class I've got cacheable methods in to reference this ? The error I am currently getting is:

No bean resolver registered in the context to resolve access to bean 

There's a similar post here talking about keys, not conditions.

Has anyone successfully managed to reference other beans in caching annotations ?

like image 744
PaulNUK Avatar asked Dec 07 '25 03:12

PaulNUK


1 Answers

Suppose that someBean in a autowired bean in your class, you can use the object being invoked target to get it, try this

@Cacheable(condition="target.someBean.isSomeBoolean()")
like image 137
NHS Avatar answered Dec 08 '25 17:12

NHS