如果指定的excpetion

时间:2015-06-03 12:24:12

标签: spring caching spring-cache

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

服务器:Tomcat 7

春季版: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$2.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 configuarion

<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 docs上给出的配置

<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();
    }

任何人都可以指出在实施缓存时可能会出现此问题的位置。

1 个答案:

答案 0 :(得分:0)

感谢StéphaneNic​​oll,我找到了问题

  

如果指定

,则缓存名称必须为非null

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

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

提供合适的名称并已解决。感谢。