对象的私有 属性 上的可缓存注释的条件

Condition on cacheable annotation on private property of Object

我有一个用例,我希望能够缓存基于对象 属性 的方法的结果。 属性 是私有的,但公开了 public getter。 (这可以更改,但我不想那样做)

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

PackageEntity class 是

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 条件缓存的文档指定了如何使用条件。但是,这种缓存并不像条件指示的那样工作。它只是缓存所有包,而不考虑售价。 我无法理解我做错了什么。也没有合适的例子可以参考。

感谢任何帮助。谢谢

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

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

所以 字段可以是 public 或者 public getter

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

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