阅读EhCacheManagerFactoryBean和EhCacheFactoryBean的 javadoc我发现:
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"/>
<bean id="locationCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"
p:cacheManager-ref="cacheManager"
p:cacheName="locationCache"/>
将创建一个缓存,然后我可以 @Autowired 进入。但我必须遗漏一些东西,因为这是我得到的:
向类 org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException 的侦听器实例发送上下文初始化事件的异常:在 ServletContext 资源 [/WEB-INF/applicationContext.xml 中定义名称为“locationCache”的 bean 创建错误]: init 方法调用失败;嵌套异常是 java.lang.NoSuchMethodError: net.sf.ehcache.Cache.(Ljava/lang/String;ILnet/sf/ehcache/store/MemoryStoreEvictionPolicy; ZLjava/lang/String;ZJJZJLnet/sf/ehcache/event/RegisteredEventListeners; Lnet/sf/ehcache/bootstrap/BootstrapCacheLoader;II)V
我试过 ehcache 1.7.2、1.6.2 和 1.5.0。
如果我<cache name="locationCache" />
在 ehcache.xml 中指定它可以工作,但我更喜欢在 applicationsContext.xml 中完成我的配置
<cache name="locationCache" />