代码之家  ›  专栏  ›  技术社区  ›  Manas Saxena

@使用方法参数的条件可缓存

  •  1
  • Manas Saxena  · 技术社区  · 7 年前

    @Cacheable 文件( https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html#condition-- ) 那个 condition 无法使用SpEL指定方法参数的条件。

    我也一直在尝试,但失败了。以下是我的方法:

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

    这是我的 Request

    public class Request{
     private String id;
    
     public String getId(){
      return this.id; 
     }
    
     public void setId(String id){
      this.id = id;
     }
    
    }
    

    但它失败了,除了spring SpEL例外,它说的是'!'无效。有人能帮我把情况弄清楚吗?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Gary Russell    7 年前

    首先,变量(参数)是 #req ,所以你需要 !#req... .

    第二,你需要代表 myStr

    把这一切放在一起。。。

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

    (注意 ).

        2
  •  0
  •   Myth    7 年前

    你必须使用- “#…等于(..)==假” 或者,

    另外,最好按照加里·拉塞尔的第一个答案来使用。