简体   繁体   中英

Condition on cacheable annotation on private property of Object

I have a use case in which I want to be able to cache the result of a method based on the property of a object. The property is private but exposes a public getter. (This can be changed, but i would not want to do that)

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

PackageEntity class is

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 for conditional caching specifies how to use condition. However, this cahcing does not works as indicated by condition. It is simply caching all packages irrespective of selling price. I am unable to understand what am I doing wrong. Neither there are suitable examples that I can refer for this.

Any help appreciated. Thanks

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

It seems SpEL also looks for public getters of the field if the specified field is private.

So the field can be either public or have a public getter

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

So the above code worked alright for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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