【问题标题】:Spring cache with reflect method使用反射方法的 Spring 缓存
【发布时间】:2013-01-22 15:45:41
【问题描述】:

我正在尝试使用缓存抽象文档中的方法访问自定义密钥生成器以生成用于缓存的密钥,但到目前为止我还没有找到任何解决方案...这里我给出我的代码

@Cacheable(value = "itemsCache", key ="T(com.ernst.transactionsAPI.IndexKeyGenerator).generate(#root.target, #root.method.name, #root.args[0])")
public List<FilialenImpl> getOutletByCustomSearchCache(SearchParametersImpl searchParameters);

我收到以下错误:

org.springframework.expression.spel.SpelEvaluationException: EL1004E:(pos 47): Method call: Method generate(com.ernst.transactionsImpl.filialen.FilialenServiceBean,java.lang.String,com.ernst.persistenceImpl.impl.SearchParametersImpl) cannot be found on com.ernst.transactionsAPI.IndexKeyGenerator type

它不反映方法名称,但它传递了方法类型。任何人都可以请教如何解决这个问题。

indexkeygenerator 中的方法看起来像

public Object generate(Object target, Method method, Object... params) {
 ...}

提前致谢,

问候, 拉贾塞卡。

【问题讨论】:

  • 一些盲目的猜测:您正在传递一个方法名称(一个字符串),其中需要一个方法;你在一个类上调用一个非静态方法。
  • 实际上我已经使用了 spel 上下文来传递一个方法(即#root.method.name)..但它并没有像我期望的那样发生。
  • 也许你可以看看this question,你可以找到一些有趣的东西来解决你面临的问题。
  • 上述解决方案看起来不错..但我试图通过使用以下代码访问密钥生成器类中的方法“T(com.ernst.transactionsAPI.IndexKeyGenerator).generate(#root.target , #root.method.name, #root.args[0])") #root.method.name 必须用对象方法反映方法,但它传递的字符串对象。

标签: java spring caching ehcache key-generator


【解决方案1】:

我在IndexKeyGenerator类中写了一个静态方法generateKey,直接调用 如以下代码所示..那么单个缓存的自定义缓存就完美了

@Cacheable(value = "itemsCache", key ="T(com.ernst.transactionsAPI.IndexKeyGenerator).generateKey(#root.target, #root.method, #root.args[0])") public List<FilialenImpl> getOutletByCustomSearchCache(SearchParametersImpl searchParameters);

不要忘记重写 SearchParameters 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    相关资源
    最近更新 更多