在 Spring4 中使用 ehcache3
Using ehcache3 with Spring4
有人可以通过 xml 使用 Spring JCacheCacheManager
帮助创建支持 CacheManager
的 Ehcache 吗?我有这样的东西。不确定如何为 Ehcache3 创建 javax.cache.CacheManager
。
<bean id="myCacheManager"
class="org.springframework.cache.jcache.JCacheCacheManager">
<property name="cacheManager" value="..." />
</bean>
谢谢!
推荐的方法是使用 org.springframework.cache.jcache.JCacheManagerFactoryBean
,您可以在其中注入 URI
、Properties
和 ClassLoader
。然后这个工厂 bean 将使用标准的 JCache Caching
class 来创建 javax.cache.CacheManager
.
对于Ehcache,URI
用于指向一个ehcache.xml
然后将配置CacheManager
。
因此扩展您的示例配置:
<bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
<property name="cacheManagerUri" value="file://path/to/ehcache.xml"/>
</bean>
<bean id="myCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
<property name="cacheManager" ref="jCacheManager" />
</bean>
有关 Ehcache 3/JCache 集成的详细信息,请参阅 the documentation。
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public JCacheCacheManager jCacheCacheManager() throws IOException {
return new JCacheCacheManager(cacheManager());
}
@Bean(destroyMethod = "close")
public javax.cache.CacheManager cacheManager() throws IOException {
XmlConfiguration xmlConfig = new XmlConfiguration(new ClassPathResource("ehcache.xml").getURL());
EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
return provider.getCacheManager(provider.getDefaultURI(), xmlConfig);
}
}
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public JCacheCacheManager jCacheCacheManager(JCacheManagerFactoryBean jCacheManagerFactoryBean){
JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
jCacheCacheManager.setCacheManager(jCacheManagerFactoryBean.getObject());
return jCacheCacheManager;
}
@Bean
public JCacheManagerFactoryBean jCacheManagerFactoryBean() throws URISyntaxException {
JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
jCacheManagerFactoryBean.setCacheManagerUri(getClass().getResource("/ehcache.xml").toURI());
return jCacheManagerFactoryBean;
}
}
有人可以通过 xml 使用 Spring JCacheCacheManager
帮助创建支持 CacheManager
的 Ehcache 吗?我有这样的东西。不确定如何为 Ehcache3 创建 javax.cache.CacheManager
。
<bean id="myCacheManager"
class="org.springframework.cache.jcache.JCacheCacheManager">
<property name="cacheManager" value="..." />
</bean>
谢谢!
推荐的方法是使用 org.springframework.cache.jcache.JCacheManagerFactoryBean
,您可以在其中注入 URI
、Properties
和 ClassLoader
。然后这个工厂 bean 将使用标准的 JCache Caching
class 来创建 javax.cache.CacheManager
.
对于Ehcache,URI
用于指向一个ehcache.xml
然后将配置CacheManager
。
因此扩展您的示例配置:
<bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
<property name="cacheManagerUri" value="file://path/to/ehcache.xml"/>
</bean>
<bean id="myCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
<property name="cacheManager" ref="jCacheManager" />
</bean>
有关 Ehcache 3/JCache 集成的详细信息,请参阅 the documentation。
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public JCacheCacheManager jCacheCacheManager() throws IOException {
return new JCacheCacheManager(cacheManager());
}
@Bean(destroyMethod = "close")
public javax.cache.CacheManager cacheManager() throws IOException {
XmlConfiguration xmlConfig = new XmlConfiguration(new ClassPathResource("ehcache.xml").getURL());
EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
return provider.getCacheManager(provider.getDefaultURI(), xmlConfig);
}
}
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public JCacheCacheManager jCacheCacheManager(JCacheManagerFactoryBean jCacheManagerFactoryBean){
JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
jCacheCacheManager.setCacheManager(jCacheManagerFactoryBean.getObject());
return jCacheCacheManager;
}
@Bean
public JCacheManagerFactoryBean jCacheManagerFactoryBean() throws URISyntaxException {
JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
jCacheManagerFactoryBean.setCacheManagerUri(getClass().getResource("/ehcache.xml").toURI());
return jCacheManagerFactoryBean;
}
}