1
votes

I read from spring @Cacheable docs (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html#condition--) that condition can specify conditions on method arguments using SpEL.

I have been trying the same out but failing. Here is my method:

public static final String myStr = "4";
@Cacheable(condition="#!req.id.equals("+myStr+")")
public Response myCall(Request req){}

and here is my Request POJO:

public class Request{
 private String id;

 public String getId(){
  return this.id; 
 }

 public void setId(String id){
  this.id = id;
 }

}

But it's failing with spring SpEL exceptions, saying that '!' is invalid. Can someone please help me out get the condition right?

2

2 Answers

4
votes

First of all, the variable (argument) is #req, so you need !#req....

Second, you need to represent myStr as a literal String in SpEL.

Putting it all together...

@Cacheable(condition="!#req.id.equals('" + myStr + "')")

(Note the single quotes around the value of myStr).

0
votes

As per my understanding, you cannot use at first # and then other operator sequentially. You have to use like - "#...equals(..) == false" Or, "#...equals(..) != true"

Also, best to use as suggested by the first answer by Gary Russell.