1

我正在尝试将 Sping Annotation 缓存与 EhCache 一起使用。因此,首先,我将依赖项添加到我的 pom.xml 并将 application-context.xml 配置为:

<ehcache:annotation-driven cache-manager="cacheManager" />

<ehcache:config cache-manager="cacheManager">
    <ehcache:evict-expired-elements interval="60" />
</ehcache:config>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation"  value="ehcache.xml"/>
</bean>

这是 ehcache.xml 配置文件:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
    <cache name="alfresco" maxElementsInMemory="10" eternal="true" overflowToDisk="false" />
</ehcache>

最后,我将 Annontation @Cachable 添加到需要缓存的方法中:

@Cacheable(value="alfresco")
public EMContents getContents(RestTemplate restTemplate, String ticket, String webScriptUrl, String em_name)

我创建了一个junit类来调用两次相同的方法,并且第二次不使用缓存(第一次不创建键值)。

它似乎工作正常,但我收到此消息:

25 Jun 2012 19:40:07  INFO EhCacheManagerFactoryBean:100 - Initializing EHCache CacheManager
25 Jun 2012 19:40:07 DEBUG ConfigurationFactory:148 - Configuring ehcache from InputStream
25 Jun 2012 19:40:07 DEBUG BeanHandler:271 - Ignoring ehcache attribute xmlns:xsi
25 Jun 2012 19:40:07 DEBUG BeanHandler:271 - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
25 Jun 2012 19:40:07 DEBUG PropertyUtil:88 - propertiesString is null.
25 Jun 2012 19:40:07 DEBUG CacheManager:605 - No disk store path defined. Skipping disk store path conflict test.
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:184 - No CacheManagerEventListenerFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:949 - No BootstrapCacheLoaderFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:923 - CacheWriter factory not configured. Skipping...
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:96 - No CacheExceptionHandlerFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:949 - No BootstrapCacheLoaderFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:923 - CacheWriter factory not configured. Skipping...
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:96 - No CacheExceptionHandlerFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:949 - No BootstrapCacheLoaderFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:923 - CacheWriter factory not configured. Skipping...
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:96 - No CacheExceptionHandlerFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:949 - No BootstrapCacheLoaderFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:923 - CacheWriter factory not configured. Skipping...
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:96 - No CacheExceptionHandlerFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:949 - No BootstrapCacheLoaderFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:923 - CacheWriter factory not configured. Skipping...
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:96 - No CacheExceptionHandlerFactory class specified. Skipping...
25 Jun 2012 19:40:07 DEBUG Cache:1183 - Initialised cache: alfresco_action
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:331 - CacheDecoratorFactory not configured. Skipping for 'alfresco_action'.
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:360 - CacheDecoratorFactory not configured for defaultCache. Skipping for 'alfresco_action'.
25 Jun 2012 19:40:07 DEBUG Cache:1183 - Initialised cache: alfresco
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:331 - CacheDecoratorFactory not configured. Skipping for 'alfresco'.
25 Jun 2012 19:40:07 DEBUG ConfigurationHelper:360 - CacheDecoratorFactory not configured for defaultCache. Skipping for 'alfresco'.
25 Jun 2012 19:40:07 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'cacheManager'

有什么线索吗?我的配置似乎有问题。

谢谢你的时间
安德里亚

4

1 回答 1

0

安德烈亚,

确保带有@Cachable 注释的bean 实例是spring 管理的,而不是您自己创建的。如果您有正确的日志,则在调用该方法时会产生类似的内容:

2012-11-16 09:40:21,731 [http-8081-2] DEBUG com.googlecode.ehcache.annotations.interceptor.EhCacheInterceptor  - Generated key '1396312488991822982' for invocation: ReflectiveMethodInvocation: public edu.wmich.gowmu.sso.dataobject.User edu.wmich.gowmu.sso.bl.UserManager.getUser(java.lang.String,java.lang.String); target is of class [edu.wmich.gowmu.sso.bl.UserManager]
于 2012-11-16T14:48:10.210 回答