繁体   English   中英

关于Object的私有属性的可缓存注释的条件

[英]Condition on cacheable annotation on private property of Object

我有一个用例,我希望能够根据对象的属性缓存方法的结果。 该物业是私人的,但暴露了公共吸气剂。 (这可以改变,但我不想这样做)

@Cacheable(cacheNames = "detailedData", key = "#id", condition = "#currentPackage.getSellingPrice() > -1")
public Map<String, Object> getDetailedTestData(int id,PackageEntity currentPackage) {
/**
some code
*/
}

PackageEntity类是

public class PackageEntity {

    private int sellingPrice;

    public int getSellingPrice() {
        return sellingPrice;
    }

    public void setSellingPrice(int sellingPrice) {
        this.sellingPrice = sellingPrice;
    }
  /**
  some other fields and their getter/setter
  */
}

用于条件缓存的Spring doc指定了如何使用条件。 但是,这种情况并不适用于条件。 无论销售价格如何,它都可以简单地缓存所有包裹。 我无法理解我做错了什么。 我没有合适的例子可以参考。

任何帮助赞赏。 谢谢

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html#cache-annotations-cacheable-condition

如果指定的字段是私有的,SpEL似乎也会查找该字段的公共getter。

因此,该领域可以是公共的,也可以是公共吸气剂

@Cacheable(cacheNames = "detailedData", key = "#id", condition ="#currentPackage.sellingPrice > -1")
public Map<String, Object> getDetailedTestData(int id,PackageEntity currentPackage) {
  /**
    some code
  */
}

所以上面的代码对我来说没问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM