【发布时间】:2012-07-08 23:04:14
【问题描述】:
如何使以下工作: - 一个spring bean,它有一个应该用@Cacheable注解缓存的方法 - 另一个为缓存创建键的 spring bean (KeyCreatorBean)。
所以代码看起来像这样。
@Inject
private KeyCreatorBean keyCreatorBean;
@Cacheable(value = "cacheName", key = "{@keyCreatorBean.createKey, #p0}")
@Override
public List<Examples> getExamples(ExampleId exampleId) {
...
但是上面的代码不起作用:它给出了以下异常:
Caused by: org.springframework.expression.spel.SpelEvaluationException:
EL1057E:(pos 2): No bean resolver registered in the context to resolve access to bean 'keyCreatorBean'
【问题讨论】:
-
试试
#{keyCreatorBean.method},而不是@keyCreatorBean.method。只是随机猜测。 -
你可以试试这个:stackoverflow.com/a/8142249/799562
<cache:annotation-driven key-generator="myKeyGenerator"/>但需要实现org.springframework.cache.interceptor.KeyGenerator。 -
我就这个问题向 SpringSource 发了一张票:jira.springsource.org/browse/SPR-9578
-
另一个相关问题:stackoverflow.com/q/5743565/1431,这里的问题在于 Spring Security 注释,但给定的解决方法似乎不适用于我的情况。