1.版本配置:
spring 3.0.5 Release + ehcache-spring-annotations-1.1.2 + tomcat6 (+ tuscany2.0.1 tuscany 对缓存配置不知有无影响。)
因为使用的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>
异常信息:
分析解决:
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时没有指定有效的枚举常量。
查找官网 http://code.google.com/p/ehcache-spring-annotations/配置说明,在 http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable找到开始查找SelfPopulatingCacheScope的配置:
但这个配置是有默认值的,所以刚开始觉得不是这个问题,以为是缺少其他包或配置错误,没有解决。后来转了一圈还是把这个配置配上去了,配成:<ehcache:annotation-driven cache-manager="ehCacheManager" self-populating-cache-scope="shared"/>结果就好了。
奇怪的是默认值为啥没起作用,因为项目使用了soa的框架tuscany,spring的配置被Tuscany代理了,所以不知跟tuscany有关。
补充说明:
另外还有一点说明,项目没用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已经内置这个依赖了。
至此,这个问题算是解决了。