3

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 ?

Community
  • 1
  • 1
PaulNUK
  • 4,774
  • 2
  • 30
  • 58
  • i guess if you send someBean as a method argument then it can be easily referenced in condition ? have you tried that ? – anurag gupta Mar 02 '15 at 15:19
  • I'll give that a go thanks. Obviously a pain having to introduce a parameter, but if it works, it works ! – PaulNUK Mar 03 '15 at 12:28

2 Answers2

1

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()")
NHS
  • 195
  • 1
  • 2
  • 13
0

Fixed by writing a method on the class in question that delegates to another bean method call. Not ideal but works fine.

PaulNUK
  • 4,774
  • 2
  • 30
  • 58