如何强迫弹簧(靴子)使用ehcahe 3

时间:2018-02-20 13:25:09

标签: java spring spring-boot ehcache

我有使用hazelcast集成的spring-boot应用程序。 现在我需要添加ehcache支持。 @EnableCaching创建Hazelcast CacheManager。我的目标是使用@Cachable和ehcache 3。

我试图手动定义Ehcache CacheManager

  @Bean
  public CacheManager ehcache() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
    cacheManager.init();
    return cacheManager;
  }

但我得到了:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'ehcache' is expected to be of type 'org.springframework.cache.CacheManager' but was actually of type 'org.ehcache.core.EhcacheManager'.

1 个答案:

答案 0 :(得分:2)

对于Ehcache 3,您必须使用JCacheCacheManager来包装Ehcache CacheManager并将其转换为Spring CacheManager。有一个例子here

@Bean
public CacheManager cacheManager() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);
    JCacheCacheManager jCacheCacheManager = new JCacheCacheManager(cacheManager);
    return jCacheCacheManager;
 }