【发布时间】:2018-11-05 03:02:46
【问题描述】:
是否可以在@Cacheable注解的unless表达式中使用spring属性占位符?我有一个想要缓存的服务方法,除非返回的结果小于 @PropertySource("classpath:application.properties") 中指定的属性 minCacheCalc 的值。
这里是我要缓存的服务类方法:
@Service
class CalculationService {
// @Cacheable(cacheNames="calculations", unless="#result < 10") // works fine hardcoded
@Cacheable(cacheNames="calculations", unless="#result < ${minCacheCalc}")
public Integer calculate(Integer i) {
System.out.println("calculate(" + i + ")");
return i * i - i;
}
}
调用这个会抛出错误:
SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'lcurly({)'
我尝试了许多不同的语法,但似乎找不到。
有没有办法在我的@Cacheable 的unless 参数中引用属性?
【问题讨论】:
-
我在这里使用了@TomCollins 的解决方法:stackoverflow.com/a/34475679/973060。恐怕我的用例可能不受支持。