【发布时间】:2015-03-11 11:23:35
【问题描述】:
我希望有动态缓存名称,并且 spring 4.1 allows that
从 Spring 4.1 开始,缓存注解的 value 属性不再是强制性的,因为无论注解的内容如何,CacheResolver 都可以提供此特定信息。
注意我是如何在所有可能的级别上偏执地设置cacheResolver:
@Cacheable(cacheResolver = "defaultCacheResolver")
@CacheConfig(cacheResolver = "defaultCacheResolver")
public interface GatewayRepository extends CrudRepository<Gateway, Integer> {
@Cacheable(cacheResolver = "defaultCacheResolver")
Gateway findByBulkId(int bulkId);
}
Spring 4.1.5 仍然无法验证配置并出现错误:Caused by: java.lang.IllegalStateException: No cache names could be detected on 'public abstract skunkworks.data.Gateway skunkworks.repos.GatewayRepository.findByBulkId(int)'. Make sure to set the value parameter on the annotation or declare a @CacheConfig at the class-level with the default cache name(s) to use.
at org.springframework.cache.annotation.SpringCacheAnnotationParser.validateCacheOperation(SpringCacheAnnotationParser.java:240)
【问题讨论】:
-
如果你删除所有这些并且在方法上只使用一个简单的@Cacheable 会发生什么?看起来注释隐藏在代理或其他东西后面。如果我是你,我会在 SD 存储库上使用 ORM 二级缓存支持。
-
普通缓存配置有效。我查看了spring代码,它正确读取了cacheResolver,但验证失败。对我来说,这看起来像是一个 Spring 错误,但太明显了。
-
我对你如何到达那里感到有点困惑。如果您有一个项目可以重现您的问题,请创建一个问题(jira.spring.io/browse/SPR),我们会看看。我们很快就会发布 4.1.6,所以请尽快发布。
标签: spring spring-data spring-cache