Spring cache 如果特定异常,缓存名称必须是非空的

Spring cache Cache name must be non-null if specified excpetion

我一直在尝试为我的项目实施基本缓存,但在部署应用程序时出现错误。

服务器:Tomcat7

Spring版本:4.1.4

Caused by: java.lang.IllegalArgumentException: Cache name must be non-null if specified
    at org.springframework.util.Assert.hasText(Assert.java:162)
    at org.springframework.cache.interceptor.CacheOperation.setCacheNames(CacheOperation.java:66)
    at org.springframework.cache.annotation.SpringCacheAnnotationParser.parseCacheableAnnotation(SpringCacheAnnotationParser.java:102)
    at org.springframework.cache.annotation.SpringCacheAnnotationParser.parseCacheAnnotations(SpringCacheAnnotationParser.java:67)
    at org.springframework.cache.annotation.SpringCacheAnnotationParser.parseCacheAnnotations(SpringCacheAnnotationParser.java:57)
    at org.springframework.cache.annotation.AnnotationCacheOperationSource.getCacheOperations(AnnotationCacheOperationSource.java:124)
    at org.springframework.cache.annotation.AnnotationCacheOperationSource.determineCacheOperations(AnnotationCacheOperationSource.java:142)
    at org.springframework.cache.annotation.AnnotationCacheOperationSource.findCacheOperations(AnnotationCacheOperationSource.java:121)
    at org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource.computeCacheOperations(AbstractFallbackCacheOperationSource.java:140)
    at org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource.getCacheOperations(AbstractFallbackCacheOperationSource.java:100)
    at org.springframework.cache.interceptor.CacheOperationSourcePointcut.matches(CacheOperationSourcePointcut.java:39)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:225)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262)
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:330)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:293)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    ... 64 more

我尝试使用不同的缓存管理器配置,但没有成功

这里是 xml 配置

<cache:annotation-driven />
    <bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
        <constructor-arg>
            <set>
                <value>cache1</value>
                <value>cache2</value>
                <value>cache3</value>
            </set>
        </constructor-arg>
    </bean>

我还尝试了 spring 文档

上给出的配置
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="cache1"/>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="cache2"/>
        </set>
    </property>
</bean>

我正在尝试缓存的函数

@Cacheable(value = "cache1", key = "#root.methodName")
    public List<ObjectDetails> getAllObjects() {
        return objectDao.getAllObjects();
    }

任何人都可以指出实施缓存时可能出现此问题的地方。

感谢 Stéphane Nicoll,我才得以找到问题

Cache name must be non-null if specified

这意味着我提供了一个空字符串来代替这样的缓存名称。

@Cacheable(value = "", key = "#root.methodName")
    public List<ObjectDetails> getAllObjects() {
        return objectDao.getAllObjects();
    }

提供了合适的名称,问题已解决。谢谢