@Cachable 使用时出现的问题
问题1: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field ‘mykey’ cannot be found on object of type ‘org.springframework.cache.interceptor.CacheExpressionRootObject’ - maybe not public?
原因:id前面也需加#
@Cacheable(value = "books", key = "#id.concat(name)") //❌
@Cacheable(value = "books", key = "#id.concat(#name)") //正确
public Book getByIsbn(Long id, String name) {
return new Book(isbn, "Some book");
}
问题2:EL1004E: Method call: Method concat(java.lang.Long) cannot be found on java.lang.Long type"
// 多参数拼接
@Cacheable(value = "books", key = "#name.concat(#id)") //❌
@Cacheable(value = "books", key = "#name+'_'+#id") //正确
public Book getByIsbn(Long id, String name) {
return new Book(isbn, "Some book");
}