<ehcache-1> SelfPopulatingCacheScope异常

[b]环境配置:[/b]
1.版本配置:
spring 3.0.5 Release + ehcache-spring-annotations-1.1.2 + tomcat6 [color=red](+ tuscany2.0.1 tuscany 对缓存配置不知有无影响。)[/color]
因为使用的spring是3.1之前版本,spring在2.5之后删除原生配置ehcache后还没添加新的支持ehcache,所以使用了ehcache-spring-annotations-1.1.2,spring3.1之后可以使用自己的ehcache支持。

2.配置文件:
application-context.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<context:property-placeholder properties-ref="jdbc"/>
<ehcache:annotation-driven cache-manager="ehCacheManager"/>
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
......


ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false" monitoring="autodetect">
<diskStore path="java.io.tmpdir" />
<defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
<cache name="userCache"
maxElementsInMemory="200"
timeToIdleSeconds="120"
timeToLiveSeconds="600"
eternal="false"
overflowToDisk="false"
maxElementsOnDisk="0"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="0"
memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>


[b]异常信息:[/b]

[img]http://dl2.iteye.com/upload/attachment/0094/7541/8c103c7d-5f3d-3090-9c50-b15ba7ba2b96.bmp[/img]

[b]分析解决:[/b]
no enum const class com.googlecode.ehcache.annotations.SelfPopulatingCacheScope.
看报错是枚举常量不存在。这个是Enum类调用valueOf()方法抛出的异常,查看jdk源码:
public static <T extends Enum<T>> T valueOf(Class<T> enumType,
String name) {
T result = enumType.enumConstantDirectory().get(name);
if (result != null)
return result;
if (name == null)
throw new NullPointerException("Name is null");
throw new IllegalArgumentException(
"No enum const " + enumType +"." + name);
}

说明是使用SelfPopulatingCacheScope时没有指定有效的枚举常量。
查找官网[url]http://code.google.com/p/ehcache-spring-annotations/[/url]配置说明,在[url]http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable[/url]找到开始查找SelfPopulatingCacheScope的配置:

[img]http://dl2.iteye.com/upload/attachment/0094/7558/30ed4444-389f-3c47-89bd-7aa2ead91b17.bmp[/img]

但这个配置是有默认值的,所以刚开始觉得不是这个问题,以为是缺少其他包或配置错误,没有解决。后来转了一圈还是把这个配置配上去了,配成:<ehcache:annotation-driven cache-manager="ehCacheManager" self-populating-cache-scope="shared"/>结果就好了。
奇怪的是默认值为啥没起作用,因为项目使用了soa的框架tuscany,spring的配置被Tuscany代理了,所以不知跟tuscany有关。

[b]补充说明:[/b]
另外还有一点说明,项目没用springMVC,所以用ehcache-spring-annotations-1.1.2还必须使用spring-context-support(如果是maven配置的话,如果构建使用的ant就直接下载把添加依赖就可以了):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>

否则启动也报错,报错“beanName”ehCacheManager is null。如果使用springMVC 3.0.*版本,则不需要单独配置spring-context-support,因为springMVC已经内置这个依赖了。
至此,这个问题算是解决了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值