尝试向现有的Spring4.3(非springboot)应用程序添加缓存
使用ehcache 3.5,会让人困惑。
beans.xml文件
<bean id="ehCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
<property name="cacheManager">
<bean class="org.springframework.cache.jcache.JCacheManagerFactoryBean"
jsr107:cacheManagerUri="classpath:ehcache.xml"/>
</property>
</bean>
mycacheservice.java文件
package com.me;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.net.URISyntaxException;
@Component
public class MyCacheService {
private static int value = 7;
@Cacheable("cache")
public int incValue() {
return value++;
}
}
pom.xml文件
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.0</version>
</dependency>
ehcache.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.4.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.4.xsd">
<service>
<jsr107:defaults enable-management="false" enable-statistics="true"/>
</service>
<cache alias="cache">
<resources>
<heap unit="entries">2000</heap>
</resources>
</cache>
</config>
在课堂上,
@Autowired
MyCacheService myCacheService;
在方法中,如果类,
System.out.println("FOO -------------" + myCacheService.incValue());
错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheManager' defined in ServletContext resource [/WEB-INF/cxf-beans.xml]: Cannot create inner bean 'org.springframework.cache.jcache.JCacheManagerFactoryBean#4b6fd0' of type [org.springframework.cache.jcache.JCacheManagerFactoryBean] while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.jcache.JCacheManagerFactoryBean#4b6fd0' defined in ServletContext resource [/WEB-INF/cxf-beans.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.terracotta.statistics.StatisticsManager.createPassThroughStatistic(Ljava/lang/Object;Ljava/lang/String;Ljava/util/Set;Lorg/terracotta/statistics/StatisticType;Ljava/util/function/Supplier;)V
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1284)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
... 62 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.jcache.JCacheManagerFactoryBean#4b6fd0' defined in ServletContext resource [/WEB-INF/cxf-beans.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.terracotta.statistics.StatisticsManager.createPassThroughStatistic(Ljava/lang/Object;Ljava/lang/String;Ljava/util/Set;Lorg/terracotta/statistics/StatisticType;Ljava/util/function/Supplier;)V
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1634)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)
... 72 more
Caused by: java.lang.NoSuchMethodError: org.terracotta.statistics.StatisticsManager.createPassThroughStatistic(Ljava/lang/Object;Ljava/lang/String;Ljava/util/Set;Lorg/terracotta/statistics/StatisticType;Ljava/util/function/Supplier;)V
at org.ehcache.impl.internal.store.heap.OnHeapStore.<init>(OnHeapStore.java:265)
at org.ehcache.impl.internal.store.heap.OnHeapStore$Provider.createStoreInternal(OnHeapStore.java:1612)
at org.ehcache.impl.internal.store.heap.OnHeapStore$Provider.createStore(OnHeapStore.java:1579)
at org.ehcache.impl.internal.store.heap.OnHeapStore$Provider.createStore(OnHeapStore.java:1560)
at org.ehcache.core.EhcacheManager.getStore(EhcacheManager.java:506)
at org.ehcache.core.EhcacheManager.createNewEhcache(EhcacheManager.java:316)
at org.ehcache.core.EhcacheManager.createCache(EhcacheManager.java:265)
at org.ehcache.core.EhcacheManager.init(EhcacheManager.java:579)
at org.ehcache.jsr107.EhcacheCachingProvider.createCacheManager(EhcacheCachingProvider.java:151)
at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:127)
at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:78)
at org.springframework.cache.jcache.JCacheManagerFactoryBean.afterPropertiesSet(JCacheManagerFactoryBean.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1692)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630)
... 75 more
更新:
我把ehcache的版本改成了3.0.0
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myCacheService' defined in file [C:\Foo\target\foo-web-service\WEB-INF\classes\com\me\MyCacheService.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'ehCacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheManager': Failed to introspect bean class [org.springframework.cache.jcache.JCacheCacheManager] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: javax/cache/CacheManager
1条答案
按热度按时间mkshixfv1#
错误是由于xmlns定义不正确造成的。当我把它改成这个的时候,一切都很好。
并称之为
p:
```<cache:annotation-driven cache-manager="ehCacheManager"/>